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

Module file for page_example_module.

Functions & methods

NameDescription
page_example_argumentsA more complex page callback that takes arguments.
page_example_descriptionConstructs a descriptive page.
page_example_helpImplements hook_help().
page_example_menuImplements hook_menu().
page_example_permissionImplements hook_permission().
page_example_simpleConstructs a simple page.

File

page_example/page_example.module
View source
  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 'example', 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. */
  133. function page_example_description() {
  134. return array('#markup' => t('The page_example provides two pages, "simple" and "arguments". The <a href="@simple_link">simple page</a> just returns a renderable array for display. The <a href="@arguments_link">arguments page</a> takes two arguments and displays them, as in @arguments_link', array('@simple_link' => url('examples/page_example/simple', array('absolute' => TRUE)), '@arguments_link' => url('examples/page_example/arguments/23/56', array('absolute' => TRUE)))));
  135. }
  136. /**
  137. * Constructs a simple page.
  138. *
  139. * The simple page callback, mapped to the path 'examples/page_example/simple'.
  140. *
  141. * Page callbacks return a renderable array with the content area of the page.
  142. * The theme system will later render and surround the content in the
  143. * appropriate blocks, navigation, and styling.
  144. *
  145. * If you do not want to use the theme system (for example for outputting an
  146. * image or XML), you should print the content yourself and not return anything.
  147. */
  148. function page_example_simple() {
  149. return array('#markup' => '<p>' . t('Simple page: The quick brown fox jumps over the lazy dog.') . '</p>');
  150. }
  151. /**
  152. * A more complex page callback that takes arguments.
  153. *
  154. * This callback is mapped to the path 'examples/page_example/arguments/%/%'.
  155. *
  156. * The % arguments are passed in from the page URL. In our hook_menu
  157. * implementation we instructed the menu system to extract the last two
  158. * parameters of the path and pass them to this function as arguments.
  159. *
  160. * This function also demonstrates a more complex render array in the returned
  161. * values. Instead of just rendering the HTML with a theme('item_list'), the
  162. * list is left unrendered, and a #theme attached to it so that it can be
  163. * rendered as late as possible, giving more parts of the system a chance to
  164. * change it if necessary.
  165. *
  166. * Consult @link http://drupal.org/node/930760 Render Arrays documentation
  167. * @endlink for details.
  168. */
  169. function page_example_arguments($first, $second) {
  170. // Make sure you don't trust the URL to be safe! Always check for exploits.
  171. if (!is_numeric($first) || !is_numeric($second)) {
  172. // We will just show a standard "access denied" page in this case.
  173. drupal_access_denied();
  174. return; // We actually don't get here.
  175. }
  176. $list[] = t("First number was @number.", array('@number' => $first));
  177. $list[] = t("Second number was @number.", array('@number' => $second));
  178. $list[] = t('The total was @number.', array('@number' => $first + $second));
  179. $render_array['page_example_arguments'] = array(
  180. '#theme' => 'item_list', // The theme function to apply to the #items
  181. '#items' => $list, // The list itself.
  182. '#title' => t('Argument Information'),
  183. );
  184. return $render_array;
  185. }
  186. /**
  187. * @} End of "defgroup page_example".
  188. */

Comments

Problem

Hi,
I have module page_example in sites/all/modules/custom and it is not working :( Page not found shows

http://localhost/drupal/examples/page_example/simple

what I am doing wrong ?

Login or register to post comments