page_example.module 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @file
  4. * Module file for page_example_module.
  5. */
  6. /**
  7. * @defgroup page_example Example: Page
  8. * @ingroup examples
  9. * @{
  10. * This example demonstrates how a module can display a page at a given URL.
  11. *
  12. * It's important to understand how the menu system works in order to
  13. * implement your own pages. See the Menu Example module for some insight.
  14. *
  15. * @see menu_example
  16. */
  17. /**
  18. * Implements hook_help().
  19. *
  20. * Through hook_help(), a module can make documentation available to the user
  21. * for the module as a whole or for specific paths. Where the help appears
  22. * depends on the $path specified.
  23. *
  24. * In the first example below, the help text will appear on the simple page
  25. * defined in hook_menu below in the region designated for help text.
  26. *
  27. * In the second example, the text will be available through the module page as
  28. * a link beside the module or on the admin help page (admin/help) in the list
  29. * of help topics using the name of the module. To specify help in the admin
  30. * section use the module name in the path as in the second case below.
  31. *
  32. * @see hook_help()
  33. */
  34. function page_example_help($path, $arg) {
  35. switch ($path) {
  36. case 'examples/page_example/simple':
  37. // Help text for the simple page registered for this path.
  38. return t('This is help text for the simple page.');
  39. case 'admin/help#page_example':
  40. // Help text for the admin section, using the module name in the path.
  41. return t("This is help text created in the page example's second case.");
  42. }
  43. }
  44. /**
  45. * Implements hook_permission().
  46. *
  47. * Since the access to our new custom pages will be granted based on
  48. * special permissions, we need to define what those permissions are here.
  49. * This ensures that they are available to enable on the user role
  50. * administration pages.
  51. */
  52. function page_example_permission() {
  53. return array(
  54. 'access simple page' => array(
  55. 'title' => t('Access simple page'),
  56. 'description' => t('Allow users to access simple page'),
  57. ),
  58. 'access arguments page' => array(
  59. 'title' => t('Access page with arguments'),
  60. 'description' => t('Allow users to access page with arguments'),
  61. ),
  62. );
  63. }
  64. /**
  65. * Implements hook_menu().
  66. *
  67. * Because hook_menu() registers URL paths for items defined by the function, it
  68. * is necessary for modules that create pages. Each item can also specify a
  69. * callback function for a given URL. The menu items returned here provide this
  70. * information to the menu system.
  71. *
  72. * We will define some menus, and their paths will be interpreted as follows:
  73. *
  74. * If the user accesses http://example.com/?q=examples/page_example/simple,
  75. * the menu system will first look for a menu item with that path. In this case
  76. * it will find a match, and execute page_example_simple().
  77. *
  78. * If the user accesses http://example.com/?q=examples/page_example/arguments,
  79. * the menu system will find no explicit match, and will fall back to the
  80. * closest match, 'examples/page_example', executing page_example_description().
  81. *
  82. * If the user accesses
  83. * http://example.com/?q=examples/page_example/arguments/1/2, the menu
  84. * system will first look for examples/page_example/arguments/1/2. Not finding
  85. * a match, it will look for examples/page_example/arguments/1/%. Again not
  86. * finding a match, it will look for examples/page_example/arguments/%/2.
  87. * Yet again not finding a match, it will look for
  88. * examples/page_example/arguments/%/%. This time it finds a match, and so will
  89. * execute page_example_arguments(1, 2). Since the parameters are passed to
  90. * the function after the match, the function can do additional checking or
  91. * make use of them before executing the callback function.
  92. *
  93. * @see hook_menu()
  94. * @see menu_example
  95. */
  96. function page_example_menu() {
  97. // This is the minimum information you can provide for a menu item. This menu
  98. // item will be created in the default menu, usually Navigation.
  99. $items['examples/page_example'] = array(
  100. 'title' => 'Page Example',
  101. 'page callback' => 'page_example_description',
  102. 'access callback' => TRUE,
  103. 'expanded' => TRUE,
  104. );
  105. $items['examples/page_example/simple'] = array(
  106. 'title' => 'Simple - no arguments',
  107. 'page callback' => 'page_example_simple',
  108. 'access arguments' => array('access simple page'),
  109. );
  110. // By using the MENU_CALLBACK type, we can register the callback for this
  111. // path without the item appearing in the menu; the admin cannot enable the
  112. // item in the menu, either.
  113. //
  114. // Notice that 'page arguments' is an array of numbers. These will be
  115. // replaced with the corresponding parts of the menu path. In this case a 0
  116. // would be replaced by 'examples', a 1 by 'page_example', and a 2 by
  117. // 'arguments.' 3 and 4 will be replaced by whatever the user provides.
  118. // These will be passed as arguments to the page_example_arguments() function.
  119. $items['examples/page_example/arguments/%/%'] = array(
  120. 'page callback' => 'page_example_arguments',
  121. 'page arguments' => array(3, 4),
  122. 'access arguments' => array('access arguments page'),
  123. 'type' => MENU_CALLBACK,
  124. );
  125. return $items;
  126. }
  127. /**
  128. * Constructs a descriptive page.
  129. *
  130. * Our menu maps this function to the path 'examples/page_example'.
  131. */
  132. function page_example_description() {
  133. return array(
  134. '#markup' =>
  135. t('<p>The page_example provides two pages, "simple" and "arguments".</p><p>The <a href="@simple_link">simple page</a> just returns a renderable array for display.</p><p>The <a href="@arguments_link">arguments page</a> takes two arguments and displays them, as in @arguments_link</p>',
  136. array(
  137. '@simple_link' => url('examples/page_example/simple', array('absolute' => TRUE)),
  138. '@arguments_link' => url('examples/page_example/arguments/23/56', array('absolute' => TRUE)),
  139. )
  140. ),
  141. );
  142. }
  143. /**
  144. * Constructs a simple page.
  145. *
  146. * The simple page callback, mapped to the path 'examples/page_example/simple'.
  147. *
  148. * Page callbacks return a renderable array with the content area of the page.
  149. * The theme system will later render and surround the content in the
  150. * appropriate blocks, navigation, and styling.
  151. *
  152. * If you do not want to use the theme system (for example for outputting an
  153. * image or XML), you should print the content yourself and not return anything.
  154. */
  155. function page_example_simple() {
  156. return array('#markup' => '<p>' . t('Simple page: The quick brown fox jumps over the lazy dog.') . '</p>');
  157. }
  158. /**
  159. * A more complex page callback that takes arguments.
  160. *
  161. * This callback is mapped to the path 'examples/page_example/arguments/%/%'.
  162. *
  163. * The % arguments are passed in from the page URL. In our hook_menu
  164. * implementation we instructed the menu system to extract the last two
  165. * parameters of the path and pass them to this function as arguments.
  166. *
  167. * This function also demonstrates a more complex render array in the returned
  168. * values. Instead of just rendering the HTML with a theme('item_list'), the
  169. * list is left unrendered, and a #theme attached to it so that it can be
  170. * rendered as late as possible, giving more parts of the system a chance to
  171. * change it if necessary.
  172. *
  173. * Consult @link http://drupal.org/node/930760 Render Arrays documentation
  174. * @endlink for details.
  175. */
  176. function page_example_arguments($first, $second) {
  177. // Make sure you don't trust the URL to be safe! Always check for exploits.
  178. if (!is_numeric($first) || !is_numeric($second)) {
  179. // We will just show a standard "access denied" page in this case.
  180. drupal_access_denied();
  181. // We actually don't get here.
  182. return;
  183. }
  184. $list[] = t("First number was @number.", array('@number' => $first));
  185. $list[] = t("Second number was @number.", array('@number' => $second));
  186. $list[] = t('The total was @number.', array('@number' => $first + $second));
  187. $render_array['page_example_arguments'] = array(
  188. // The theme function to apply to the #items.
  189. '#theme' => 'item_list',
  190. // The list itself.
  191. '#items' => $list,
  192. '#title' => t('Argument Information'),
  193. );
  194. return $render_array;
  195. }
  196. /**
  197. * @} End of "defgroup page_example".
  198. */