custompage.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/custompage/custompage.module
  • Custom Page Module

*

Functions & methods

NameDescription
custompage_blockGenerate custom blocks
custompage_clearcache_message
custompage_context_pluginsMake module compatible with context 3 Implement hook_context_plugins().
custompage_context_registryMake module compatible with context 3 Implement hook_context_registry().
custompage_ctools_plugin_apiMake module compatible with context 3 implement hook_ctools_plugin_api().
custompage_delegateFind a theme function and/or template file and return content
custompage_flushcacheFlush caches.
custompage_init
custompage_menuImplementation of hook_menu().
custompage_permImplementation of hook_perm().
custompage_prefix
custompage_themeImplementation of hook_theme().
custompage_uri_load
_custompage_get_mappings

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Custom Page Module
  5. **/
  6. function custompage_init() {
  7. $mpath = dirname(__FILE__);
  8. require_once ( $mpath . '/custompage_util.inc');
  9. if (module_exists('context')) { //Context module integration
  10. include_once ( $mpath . '/custompage.context.inc');
  11. }
  12. /*
  13. Load CSS. Since util functions are useful
  14. and can be called from non-custompage pages
  15. as well (e.g. a block), we need to always load
  16. css, not - just when we are on a custom page.
  17. */
  18. $mod_uri = drupal_get_path('module', 'custompage');
  19. drupal_add_css ( $mod_uri.'/custompage.css');
  20. //For Development Only. Do NOT un-comment in production
  21. //drupal_flush_all_caches();
  22. }
  23. /**
  24. * Implementation of hook_perm().
  25. */
  26. function custompage_perm() {
  27. return array(
  28. 'administer custompage',
  29. 'edit custompage tiles',
  30. );
  31. }
  32. /**
  33. * Implementation of hook_menu().
  34. *
  35. * Register menu callbacks for all custom page URLs.
  36. */
  37. function custompage_menu() {
  38. $items = array();
  39. $items['admin/build/custompage/flushcache'] = array(
  40. 'title' => 'Flush Cache',
  41. 'page callback' => 'custompage_flushcache',
  42. 'access arguments' => array('administer custompage'),
  43. 'type' => MENU_CALLBACK
  44. );
  45. $items['admin/build/custompage/settings'] = array(
  46. 'title' => 'Custom Page Settings',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('custompage_settings'),
  49. 'access arguments' => array('administer custompage'),
  50. 'file' => 'custompage.admin.inc',
  51. );
  52. $styled_pathes = _custompage_get_mappings();
  53. foreach ( $styled_pathes as $path ) {
  54. // only create callbacks for page-type components
  55. if ($path->type == 'block') continue;
  56. if ( $path->enabled ) {
  57. $access = array('access content');
  58. } else {
  59. $access = array('administer custompage');
  60. }
  61. $items[$path->path] = array(
  62. 'title' => t($path->title),
  63. 'page callback' => 'custompage_delegate',
  64. 'page arguments' => array($path->key),
  65. 'access arguments' => $access,
  66. 'type' => MENU_NORMAL_ITEM
  67. );
  68. }
  69. return $items;
  70. }
  71. /**
  72. * Generate custom blocks
  73. * @param op the operation from the URL
  74. * @param delta offset
  75. * @param edit edited values if a config or save
  76. * @returns block HTML
  77. */
  78. function custompage_block($op = 'list', $delta = 0, $edit = array())
  79. {
  80. $styled_pathes = _custompage_get_mappings();
  81. if (!is_array($styled_pathes)) $styled_pathes = array();
  82. if ($op == "list")
  83. {
  84. $blocks = array();
  85. foreach ( $styled_pathes as $path ) {
  86. // only create callbacks for page-type components
  87. if ($path->type != 'block') continue;
  88. $blocks[$path->key] = array(
  89. 'info' => $path->title,
  90. );
  91. }
  92. return $blocks;
  93. }
  94. else if ($op == 'view') {
  95. $block = array();
  96. foreach ( $styled_pathes as $path ) {
  97. // only create callbacks for page-type components
  98. if ($path->type != 'block') continue;
  99. if ($delta == $path->key ) {
  100. $block = array(
  101. 'subject' => $path->title,
  102. 'content' => theme( custompage_prefix($path->key), FALSE),
  103. );
  104. }
  105. }
  106. return $block;
  107. }
  108. }
  109. /**
  110. * Flush caches.
  111. */
  112. function custompage_flushcache() {
  113. drupal_flush_all_caches();
  114. drupal_set_message('Cache cleared.');
  115. drupal_goto($_SERVER['HTTP_REFERER']);
  116. }
  117. /**
  118. * Implementation of hook_theme().
  119. *
  120. * Register theme tpl/functions for each custompage URL.
  121. */
  122. function custompage_theme() {
  123. $styled_pathes = _custompage_get_mappings();
  124. $themes = array();
  125. foreach ( $styled_pathes as $path ) {
  126. $key = custompage_prefix($path->key);
  127. $themes[$key] = array(
  128. 'arguments' => array('title' => '', 'user' => '', 'data' => ''),
  129. 'template' => $key,
  130. //'function' => $key,
  131. );
  132. }
  133. return $themes;
  134. }
  135. /**
  136. * @return
  137. * The custompage key configured for theme prefix if necessary
  138. */
  139. function custompage_prefix($key) {
  140. $prefix = variable_get('custompage_theme_prefix', '');
  141. if(!empty($prefix)) {
  142. return "{$prefix}_{$key}";
  143. }
  144. return $key;
  145. }
  146. /**
  147. * Find a theme function and/or template file and return content
  148. */
  149. function custompage_delegate( $arg ) {
  150. global $user;
  151. //Context integration
  152. if (module_exists('context')) {
  153. // context 2
  154. if (function_exists('context_set_by_condition')) {
  155. context_set_by_condition('custompage', $arg);
  156. }
  157. // context 3
  158. else {
  159. if ($plugin = context_get_plugin('condition', 'custompage')) {
  160. $plugin->execute($arg);
  161. }
  162. }
  163. }
  164. // Let's prepare some data for this baby.
  165. $data = module_invoke_all( 'customdata_'.$arg );
  166. $key = custompage_prefix($arg);
  167. $themed = theme($key, drupal_get_title(), $user, $data);
  168. //Remove ANNOYING errors about not finding functions that are optional anyway
  169. $messages = drupal_get_messages( 'error', TRUE);
  170. if ( is_array( $messages ) & is_array( $messages['error'] ) ) {
  171. foreach ( $messages['error'] as $msg ) {
  172. $pattern = '/warning: call_user_func_array.+?First argument is expected to be a valid callback.+?/ims';
  173. if ( preg_match( $pattern, $msg ) ) {
  174. continue;
  175. }
  176. drupal_set_message( $msg, 'error');
  177. }
  178. }
  179. if ( trim ($themed) == "" ) {
  180. drupal_set_message ( "custompage could not find an appropriate theming function or template file for this path [$arg].
  181. <br /><b>The viable options (in ascending priority) are:</b>
  182. <ul>
  183. <li>'phptemplate_$arg' in any module
  184. <li>'THEMENAME_$arg' in a theme with the name THEMENAME
  185. <li> " . custompage_prefix($arg) . ".tpl.php template file in a current theme
  186. </ul>
  187. Please make sure at least one of these exist and returns a non-empty output".custompage_clearcache_message() , 'error' );
  188. return "&nbsp;"; //Returning empty leads to undesired effect of getting a blank page
  189. } else {
  190. return $themed;
  191. }
  192. }
  193. function custompage_clearcache_message() {
  194. return "<div style=\"border-top: 1px solid silver;\"><b>ATTENTION:</b> Theme engine is strongly cached in Drupal versions 6 and up and
  195. if/when you add a new tpl.php or a theming function, you need to ".l(t('clear cache'), 'admin/build/custompage/flushcache')." before you can see any changes.</div>";
  196. }
  197. function custompage_uri_load($id) {
  198. return TRUE;
  199. }
  200. function _custompage_get_mappings() {
  201. static $mappings;
  202. if ( is_array( $mappings ) ) { //performance optimization
  203. return $mappings;
  204. }
  205. $mappings = module_invoke_all('custompages');
  206. /*** Structure:
  207. $obj = new stdClass();
  208. $obj->uri = 'homepage';
  209. $obj->title = 'Home Page';
  210. $obj->key = 'homepage';
  211. $mappings[] = $obj;
  212. **/
  213. return $mappings;
  214. }
  215. /**
  216. * Make module compatible with context 3
  217. * implement hook_ctools_plugin_api().
  218. */
  219. function custompage_ctools_plugin_api($module, $api) {
  220. // site using context 2, but using ctools
  221. if (function_exists('context_set_by_condition')) {
  222. return;
  223. }
  224. if ($module == 'context' && $api == 'plugins') {
  225. return array('version' => 3);
  226. }
  227. }
  228. /**
  229. * Make module compatible with context 3
  230. * Implement hook_context_plugins().
  231. */
  232. function custompage_context_plugins() {
  233. $plugins = array();
  234. $plugins['custompage_context_condition_alias'] = array(
  235. 'handler' => array(
  236. 'path' => drupal_get_path('module', 'custompage') . '/plugins',
  237. 'file' => 'custompage_context_condition_alias.inc',
  238. 'class' => 'custompage_context_condition_alias',
  239. 'parent' => 'context_condition',
  240. ),
  241. );
  242. return $plugins;
  243. }
  244. /**
  245. * Make module compatible with context 3
  246. * Implement hook_context_registry().
  247. */
  248. function custompage_context_registry() {
  249. $registry['conditions'] = array(
  250. 'custompage' => array(
  251. 'title' => t('Custompage'),
  252. 'description' => t('Set condition on visiting a custom page URL.'),
  253. 'plugin' => 'custompage_context_condition_alias',
  254. ),
  255. );
  256. return $registry;
  257. }