page_example.module

  1. examples
    1. 6 page_example/page_example.module
    2. 7 page_example/page_example.module
    3. 8 page_example/page_example.module
  2. drupal
    1. 4.6 developer/examples/page_example.module
    2. 4.7 developer/examples/page_example.module
    3. 5 developer/examples/page_example.module

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

Functions & methods

NameDescription
page_example_bazA more complex page callback that takes arguments.
page_example_fooA simple page callback.
page_example_helpImplementation of hook_help().
page_example_menuImplementation of hook_menu().
page_example_permImplementation of hook_perm().

File

developer/examples/page_example.module
View source
  1. /**
  2. * @file
  3. * This is an example outlining how a module can be used to display a
  4. * custom page at a given URL.
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. *
  9. * Throughout Drupal, hook_help() is used to display help text at the top of
  10. * pages. Some other parts of Drupal pages get explanatory text from these hooks
  11. * as well. We use it here to provide a description of the module on the
  12. * module administration page. This example also illustrates how to add help
  13. * text to the pages your module defines.
  14. */
  15. function page_example_help($section) {
  16. switch ($section) {
  17. case 'admin/modules#description':
  18. // This description is shown in the listing at admin/modules.
  19. return t('An example module showing how to define a page to be displayed to the user at a given URL.');
  20. case 'foo':
  21. // Here is some help text for a custom page.
  22. return t('This sentence contains all the letters in the English alphabet.');
  23. }
  24. }
  25. /**
  26. * Implementation of hook_perm().
  27. *
  28. * Since the access to our new custom pages will be granted based on
  29. * special permissions, we need to define what those permissions are here.
  30. * This ensures that they are available to enable on the user role
  31. * administration pages.
  32. */
  33. function page_example_perm() {
  34. return array('access foo', 'access baz');
  35. }
  36. /**
  37. * Implementation of hook_menu().
  38. *
  39. * You must implement hook_menu() to emit items to place in the main menu.
  40. * This is a required step for modules wishing to display their own pages,
  41. * because the process of creating the links also tells Drupal what
  42. * callback function to use for a given URL. The menu items returned
  43. * here provide this information to the menu system.
  44. *
  45. * With the below menu definitions, URLs will be interpreted as follows:
  46. *
  47. * If the user accesses http://example.com/?q=foo, then the menu system
  48. * will first look for a menu item with that path. In this case it will
  49. * find a match, and execute page_example_foo().
  50. *
  51. * If the user accesses http://example.com/?q=bar, no match will be found,
  52. * and a 404 page will be displayed.
  53. *
  54. * If the user accesses http://example.com/?q=bar/baz, the menu system
  55. * will find a match and execute page_example_baz().
  56. *
  57. * If the user accesses http://example.com/?q=bar/baz/1/2, the menu system
  58. * will first look for bar/baz/1/2. Not finding a match, it will look for
  59. * bar/baz/1. Again not finding a match, it will look for bar/baz. This
  60. * time it finds a match, and so will execute page_example_baz(1,2). Note
  61. * the parameters being passed; this is a very useful technique.
  62. *
  63. * If the user accesses http://example.com/?q=bar/baz/52/97, the menu system
  64. * finds a match, but since its callback is absent, it proceeds
  65. * as above and ends up calling page_example_baz(52,97) nonetheless.
  66. */
  67. function page_example_menu($may_cache) {
  68. $items = array();
  69. // The $may_cache parameter is used to divide menu items into two parts. Those
  70. // returned when $may_cache is true must be consistently applicable for the
  71. // current user at all times; the others may change or be defined at only
  72. // certain paths. Most modules will have excusively cacheable menu items.
  73. if ($may_cache) {
  74. // This is the minimum information you can provide for a menu item.
  75. $items[] = array('path' => 'foo', 'title' => t('foo'),
  76. 'callback' => 'page_example_foo',
  77. 'access' => user_access('access foo'));
  78. // By using the MENU_CALLBACK type, we can register the callback for this
  79. // path but not have the item show up in the menu; the admin is not allowed
  80. // to enable the item in the menu, either.
  81. $items[] = array('path' => 'bar/baz', 'title' => t('baz'),
  82. 'callback' => 'page_example_baz',
  83. 'access' => user_access('access baz'),
  84. 'type' => MENU_CALLBACK);
  85. // Here is a menu item that doesn't register a callback. By default, the
  86. // attributes are inherited from the parent menu item. In this case, the
  87. // permissions of the parent suffice but we to override the title if
  88. // they enter some "magic" parameters. Note: if you remove the 'type'
  89. // attribute, the item will appear in the menu.
  90. $items[] = array('path' => 'bar/baz/52/97',
  91. 'title' => t('the magic numbers'),
  92. 'type' => MENU_CALLBACK);
  93. }
  94. return $items;
  95. }
  96. /**
  97. * A simple page callback.
  98. *
  99. * Page callbacks are required to return the entire page. The content
  100. * is then usually output via a call to theme('page'), where the theme system
  101. * will then surround the content in the appropriate blocks, navigation, and
  102. * styling.
  103. *
  104. * If you do not want to use the theme system (for example for outputting an
  105. * image or XML), you should print the content yourself and not return anything.
  106. */
  107. function page_example_foo() {
  108. $content = '<p>The quick brown fox jumps over the lazy dog.</p>';
  109. return $content;
  110. }
  111. /**
  112. * A more complex page callback that takes arguments.
  113. *
  114. * The arguments are passed in from the page URL. They are always the next
  115. * elements of the path after the page location. Because of this, if the
  116. * URL of the page is moved later, this function does not need to be changed
  117. * to accomodate the move. It's a good idea to always provide default values
  118. * for the parameters
  119. */
  120. function page_example_baz($alice = 0, $bob = 0) {
  121. // Make sure you don't trust the URL to be safe! Always check for exploits.
  122. if (!is_numeric($alice) || !is_numeric($bob)) {
  123. // We will just show a standard "access denied" page in this case.
  124. drupal_access_denied();
  125. return;
  126. }
  127. $list[] = "Alice's number was $alice.";
  128. $list[] = "Bob's number was $bob.";
  129. $list[] = 'The total was '. ($alice + $bob) .'.';
  130. $content = theme('item_list', $list);
  131. return $content;
  132. }
Login or register to post comments