Community Documentation

page_example.module

  1. examples
    1. 6 page_example.module
    2. 7 page_example.module
    3. 8 page_example.module
  2. drupal
    1. 4.6 page_example.module
    2. 4.7 page_example.module
    3. 5 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().
View source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

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

/**
 * Implementation of hook_help().
 *
 * Throughout Drupal, hook_help() is used to display help text at the top of
 * pages. Some other parts of Drupal pages get explanatory text from these hooks
 * as well. We use it here to illustrate how to add help text to the pages your
 * module defines.
 */
function page_example_help($section) {
  switch ($section) {
    case 'foo':
      // Here is some help text for a custom page.
      return t('This sentence contains all the letters in the English alphabet.');
  }
}

/**
 * Implementation of hook_perm().
 *
 * Since the access to our new custom pages will be granted based on
 * special permissions, we need to define what those permissions are here.
 * This ensures that they are available to enable on the user role
 * administration pages.
 */
function page_example_perm() {
  return array('access foo', 'access baz');
}

/**
 * Implementation of hook_menu().
 *
 * You must implement hook_menu() to emit items to place in the main menu.
 * This is a required step for modules wishing to display their own pages,
 * because the process of creating the links also tells Drupal what
 * callback function to use for a given URL. The menu items returned
 * here provide this information to the menu system.
 *
 * With the below menu definitions, URLs will be interpreted as follows:
 *
 * If the user accesses http://example.com/?q=foo, then the menu system
 * will first look for a menu item with that path. In this case it will
 * find a match, and execute page_example_foo().
 *
 * If the user accesses http://example.com/?q=bar, no match will be found,
 * and a 404 page will be displayed.
 *
 * If the user accesses http://example.com/?q=bar/baz, the menu system
 * will find a match and execute page_example_baz().
 *
 * If the user accesses http://example.com/?q=bar/baz/1/2, the menu system
 * will first look for bar/baz/1/2. Not finding a match, it will look for
 * bar/baz/1. Again not finding a match, it will look for bar/baz. This
 * time it finds a match, and so will execute page_example_baz(1,2). Note
 * the parameters being passed; this is a very useful technique.
 *
 * If the user accesses http://example.com/?q=bar/baz/52/97, the menu system
 * finds a match, but since its callback is absent, it proceeds
 * as above and ends up calling page_example_baz(52,97) nonetheless.
 */
function page_example_menu($may_cache) {
  $items = array();

  // The $may_cache parameter is used to divide menu items into two parts. Those
  // returned when $may_cache is true must be consistently applicable for the
  // current user at all times; the others may change or be defined at only
  // certain paths. Most modules will have excusively cacheable menu items.
  if ($may_cache) {
    // This is the minimum information you can provide for a menu item.
    $items[] = array('path' => 'foo', 'title' => t('foo'),
      'callback' => 'page_example_foo',
      'access' => user_access('access foo'));

    // By using the MENU_CALLBACK type, we can register the callback for this
    // path but not have the item show up in the menu; the admin is not allowed
    // to enable the item in the menu, either.
    $items[] = array('path' => 'bar/baz', 'title' => t('baz'),
      'callback' => 'page_example_baz',
      'access' => user_access('access baz'),
      'type' => MENU_CALLBACK);

    // Here is a menu item that doesn't register a callback. By default, the
    // attributes are inherited from the parent menu item. In this case, the
    // permissions of the parent suffice but we to override the title if
    // they enter some "magic" parameters. Note: if you remove the 'type'
    // attribute, the item will appear in the menu.
    $items[] = array('path' => 'bar/baz/52/97',
      'title' => t('the magic numbers'),
      'type' => MENU_CALLBACK);
  }

  return $items;
}

/**
 * A simple page callback.
 *
 * Page callbacks are required to return the entire page. The content
 * is then usually output via a call to theme('page'), where the theme system
 * will then surround the content in the appropriate blocks, navigation, and
 * styling.
 *
 * If you do not want to use the theme system (for example for outputting an
 * image or XML), you should print the content yourself and not return anything.
 */
function page_example_foo() {
  $content = '<p>The quick brown fox jumps over the lazy dog.</p>';
  return $content;
}

/**
 * A more complex page callback that takes arguments.
 *
 * The arguments are passed in from the page URL. They are always the next
 * elements of the path after the page location. Because of this, if the
 * URL of the page is moved later, this function does not need to be changed
 * to accomodate the move. It's a good idea to always provide default values
 * for the parameters
 */
function page_example_baz($alice = 0, $bob = 0) {
  // Make sure you don't trust the URL to be safe! Always check for exploits.
  if (!is_numeric($alice) || !is_numeric($bob)) {
    // We will just show a standard "access denied" page in this case.
    drupal_access_denied();
    return;
  }

  $list[] = "Alice's number was $alice.";
  $list[] = "Bob's number was $bob.";
  $list[] = 'The total was '. ($alice + $bob) .'.';
  $content = theme('item_list', $list);
  return $content;
}
Login or register to post comments