page_example.module

  1. 7 page_example/page_example.module
  2. 6 page_example/page_example.module
  3. 8 page_example/page_example.module

This is an example outlining how a module can be used to display a custom page at a given URL.

Functions

Namesort descending Description
page_example_arguments A more complex page callback that takes arguments.
page_example_custom_access_callback If your custom access callback function checks for permissions, it should still use user_access. You may add whatever additional logic you like, though.
page_example_custom_access_page Page callback to demonstrate a custom access callback.
page_example_description None of these page examples makes use of the Form API. The Form Example provides examples that do.
page_example_help Implementation of hook_help().
page_example_menu Implementation of hook_menu().
page_example_other_permissions Page callback to demonstrate checking for the 'access content' permission.
page_example_perm Implementation of hook_perm().
page_example_simple A simple page callback.

File

page_example/page_example.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This is an example outlining how a module can be used to display a
  5. * custom page at a given URL.
  6. */
  7. /**
  8. * @defgroup page_example Example: Page
  9. * @ingroup examples
  10. * @{
  11. * Create a page in a module. (Drupal 6)
  12. *
  13. * This example demonstrates how a module can be used to display a
  14. * custom page at a given URL.
  15. *
  16. * This example is part of the Examples for Developers Project
  17. * which you can download and experiment with here:
  18. * http://drupal.org/project/examples
  19. */
  20. /**
  21. * Implementation of hook_help().
  22. */
  23. function page_example_help($path, $arg) {
  24. switch ($path) {
  25. case 'admin/help#page_example':
  26. $output = page_example_description();
  27. $output .= '<p>';
  28. $output .= t("The simple page requires 'access simple page'
  29. permission. The different permissions page requires
  30. 'access content' permission. The custom access
  31. callback page requires 'access content' permission
  32. and the visitor to be signed in. The arguments page
  33. requires 'access arguments page' permission.");
  34. $output .= '</p>';
  35. return $output;
  36. }
  37. }
  38. /**
  39. * Implementation of hook_perm().
  40. *
  41. * Since the access to our new custom pages will be granted based on
  42. * special permissions, we need to define what those permissions are here.
  43. * This ensures that they are available to enable on the user role
  44. * administration pages.
  45. */
  46. function page_example_perm() {
  47. // Defining permissions for your module is as simple as returning an array of
  48. // strings.
  49. return array(
  50. 'access simple page',
  51. 'access arguments page',
  52. );
  53. }
  54. /**
  55. * Implementation of hook_menu().
  56. *
  57. * hook_menu() must be implemented to emit items to place in the main menu.
  58. * This is a required step for modules wishing to display their own pages,
  59. * because the process of creating the links also tells Drupal what
  60. * callback function to use for a given URL. The menu items returned
  61. * here provide this information to the menu system.
  62. *
  63. * With the below menu definitions, URLs will be interpreted as follows:
  64. *
  65. * Note that the URLs below are written as though Clean URLs are not
  66. * enabled on the site.
  67. *
  68. * If the user accesses http://example.com/?q=examples/page_example, then the
  69. * menu system will look for a menu item with that path. In this case it will
  70. * find a match, and execute page_example_description().
  71. *
  72. * If the user accesses http://example.com/?q=examples/page_example/simple but
  73. * does not have the required 'access simple page' permission, access will be
  74. * denied.
  75. *
  76. * If http://example.com/?q=examples/page_example/arguments/1/2 is accessed,
  77. * the menu system will first look for examples/page_example/arguments/1/2. Not
  78. * finding a match, it will look for examples/page_example/arguments/1/%. Again
  79. * not finding a match, it will look for examples/page_example/arguments/%/2.
  80. * Yet again not finding a match, it will look for example/arguments/%/%. This
  81. * time it finds a match, and so will execute page_example_arguments(1, 2).
  82. * Note the parameters being passed; this is a very useful technique.
  83. *
  84. * If http://example.com/?q=examples/page_example/arguments/1 is accessed, the
  85. * menu system will match 'examples/page_example', and not
  86. * 'examples/page_example/arguments/%/%'. The menu entries for the latter
  87. * require 5 arguments. All the elements included in the menu entry definition
  88. * should be present for the menu system to match the request.
  89. *
  90. * If the user accesses
  91. * http://example.com/?q=examples/page_example/different-permission but
  92. * does not have the required 'access content' permission, access will be
  93. * denied.
  94. *
  95. * If the user accesses
  96. * http://example.com/?q=examples/page_example/custom-access-callback but
  97. * does not have the required 'access content' permission and/or
  98. * is not signed in, access will be denied.
  99. *
  100. * The @link menu_example.module Menu Example @endlink provides more extensive
  101. * examples for hook_menu().
  102. */
  103. function page_example_menu() {
  104. // This is the minimum information you can provide for a menu item. This menu
  105. // item will be created in the default menu (Navigation).
  106. // Specifying 'access callback' => TRUE causes this menu item to be
  107. // available for all roles.
  108. $items['examples/page_example'] = array(
  109. 'title' => 'Page Example',
  110. 'page callback' => 'page_example_description',
  111. 'access callback' => TRUE,
  112. 'expanded' => TRUE,
  113. );
  114. // This example checks if the user has permission to access the page. Users
  115. // that do not have the 'access simple page' permission will be denied. An
  116. // array containing the required permissions is given in 'access arguments'
  117. // and here we use the default 'access callback', which is 'user_access'.
  118. $items['examples/page_example/simple'] = array(
  119. 'title' => 'Simple - no arguments',
  120. 'page callback' => 'page_example_simple',
  121. 'access arguments' => array('access simple page'),
  122. );
  123. // By using the MENU_CALLBACK type, we can register the callback for this
  124. // path but not have the item show up in the menu.
  125. //
  126. // Notice that the 'page arguments' is an array of numbers. These will be
  127. // replaced with the corresponding parts of the menu path. In this case a 0
  128. // would be replaced by 'examples', a 1 by 'page_example', a 2 by 'arguments'
  129. // and likewise 3 and 4 will be replaced by whatever the user provides. We
  130. // will pass these last two as arguments to the page_example_arguments()
  131. // function.
  132. //
  133. // Only users with the 'access arguments page' permission have access.
  134. $items['examples/page_example/arguments/%/%'] = array(
  135. 'title' => 'Page example with arguments',
  136. 'page callback' => 'page_example_arguments',
  137. 'page arguments' => array(3, 4),
  138. 'access arguments' => array('access arguments page'),
  139. 'type' => MENU_CALLBACK,
  140. );
  141. // You are not limited to checking for permissions defined by
  142. // this module. You can also check for permissions defined by
  143. // other modules. In this case, we will check for permissions
  144. // defined by node.module
  145. $items['examples/page_example/different-permission'] = array(
  146. 'title' => 'Page example checking for other permissions',
  147. 'page callback' => 'page_example_other_permissions',
  148. 'access arguments' => array('access content'),
  149. );
  150. // You can also define your own access callback function.
  151. $items['examples/page_example/custom-access-callback'] = array(
  152. 'title' => 'Page example using a custom access callback',
  153. 'page callback' => 'page_example_custom_access_page',
  154. 'access callback' => 'page_example_custom_access_callback',
  155. );
  156. return $items;
  157. }
  158. /**
  159. * None of these page examples makes use of the Form API.
  160. * The @link form_example.module Form Example @endlink provides
  161. * examples that do.
  162. */
  163. function page_example_description() {
  164. $output = '<p>';
  165. $output .= t('The page_example provides four pages, "simple", "arguments",
  166. "different permissions" and "custom access callback".');
  167. $output .= '</p>';
  168. $output .= '<p>';
  169. $output .= t('The <a href="@simple_link">simple page</a>,
  170. <a href="@dp_link">different permissions page</a> and
  171. <a href="@cac_link">custom access callback page</a>
  172. just return a string for display.
  173. The <a href="@arguments_link">arguments page</a> takes
  174. two arguments and displays them,
  175. as in <a href="@arguments_link">@arguments_link</a>.',
  176. array(
  177. '@simple_link' => url('examples/page_example/simple',
  178. array('absolute' => TRUE)),
  179. '@dp_link' => url('examples/page_example/different-permission',
  180. array('absolute' => TRUE)),
  181. '@cac_link' => url('examples/page_example/custom-access-callback',
  182. array('absolute' => TRUE)),
  183. '@arguments_link' => url('examples/page_example/arguments/23/56',
  184. array('absolute' => TRUE))));
  185. $output .= '</p>';
  186. $output .= '<p>';
  187. $output .= t('Note also that because these pages use permissions to
  188. limit access to the content, unprivileged or anonymous users
  189. will receive \'Access Denied\' errors.');
  190. $output .= '</p>';
  191. return $output;
  192. }
  193. /**
  194. * A simple page callback.
  195. *
  196. * Page callbacks are required to return the entire page. The content
  197. * is then usually output via a call to theme('page'), where the theme system
  198. * will then surround the content in the appropriate blocks, navigation, and
  199. * styling.
  200. *
  201. * If you do not want to use the theme system (for example for outputting an
  202. * image or XML), you should print the content yourself and not return anything.
  203. */
  204. function page_example_simple() {
  205. return '<p>' . t('Simple page: The quick brown fox jumps over the lazy dog.') . '</p>';
  206. }
  207. /**
  208. * A more complex page callback that takes arguments.
  209. *
  210. * The arguments are passed in from the page URL. In our hook_menu
  211. * implementation we instructed the menu system to extract the last two
  212. * parameters of the path and pass them to this function as arguments.
  213. */
  214. function page_example_arguments($first, $second) {
  215. // Make sure you don't trust the arguments to be safe! Always check for exploits.
  216. if (!is_numeric($first) || !is_numeric($second)) {
  217. // We will just show a standard "access denied" page in this case.
  218. drupal_access_denied();
  219. return; // We actually don't get here.
  220. }
  221. $list[] = t("First number was @number.", array('@number' => $first));
  222. $list[] = t("Second number was @number.", array('@number' => $second));
  223. $list[] = t('The total was @number.', array('@number' => $first + $second));
  224. return theme('item_list', $list);
  225. }
  226. /**
  227. * Page callback to demonstrate checking for the 'access content' permission.
  228. */
  229. function page_example_other_permissions() {
  230. return '<p>' . t('Evidently, you are authorized to view content on the site.') . '</p>';
  231. }
  232. /**
  233. * If your custom access callback function checks
  234. * for permissions, it should still use user_access.
  235. * You may add whatever additional logic you like, though.
  236. */
  237. function page_example_custom_access_callback() {
  238. global $user;
  239. return user_access('access simple page') && $user->uid > 0;
  240. }
  241. /**
  242. * Page callback to demonstrate a custom access callback.
  243. */
  244. function page_example_custom_access_page() {
  245. return '<p>' . t('Welcome to the custom access page, registered visitor.') . '</p>';
  246. }
  247. /**
  248. * @} End of "defgroup page_example".
  249. */

Comments