| 1 |
<?php
|
| 2 |
// $Id: page_example.module,v 1.14 2009/02/13 04:00:38 karschsp Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This is an example outlining how a module can be used to display a
|
| 7 |
* custom page at a given URL.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*
|
| 13 |
* Throughout Drupal, hook_help() is used to display help text at the top of
|
| 14 |
* pages. Some other parts of Drupal pages get explanatory text from these hooks
|
| 15 |
* as well. We use it here to illustrate how to add help text to the pages your
|
| 16 |
* module defines.
|
| 17 |
*/
|
| 18 |
function page_example_help($path, $arg) {
|
| 19 |
switch ($path) {
|
| 20 |
case 'admin/help#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 |
/**
|
| 27 |
* Implementation of hook_perm().
|
| 28 |
*
|
| 29 |
* Since the access to our new custom pages will be granted based on
|
| 30 |
* special permissions, we need to define what those permissions are here.
|
| 31 |
* This ensures that they are available to enable on the user role
|
| 32 |
* administration pages.
|
| 33 |
*/
|
| 34 |
function page_example_perm() {
|
| 35 |
return array(
|
| 36 |
'administer foo' => array(
|
| 37 |
'title' => t('Administer foo module'),
|
| 38 |
'description' => t('Allow users to administer foo settings'),
|
| 39 |
),
|
| 40 |
'access foo content' => array(
|
| 41 |
'title' => t('Access foo content'),
|
| 42 |
'description' => t('Allow users to access foo content'),
|
| 43 |
),
|
| 44 |
);
|
| 45 |
}
|
| 46 |
|
| 47 |
/**
|
| 48 |
* Implementation of hook_menu().
|
| 49 |
*
|
| 50 |
* You must implement hook_menu() to emit items to place in the main menu.
|
| 51 |
* This is a required step for modules wishing to display their own pages,
|
| 52 |
* because the process of creating the links also tells Drupal what
|
| 53 |
* callback function to use for a given URL. The menu items returned
|
| 54 |
* here provide this information to the menu system.
|
| 55 |
*
|
| 56 |
* With the below menu definitions, URLs will be interpreted as follows:
|
| 57 |
*
|
| 58 |
* If the user accesses http://example.com/?q=foo, then the menu system
|
| 59 |
* will first look for a menu item with that path. In this case it will
|
| 60 |
* find a match, and execute page_example_foo().
|
| 61 |
*
|
| 62 |
* If the user accesses http://example.com/?q=bar, no match will be found,
|
| 63 |
* and a 404 page will be displayed.
|
| 64 |
*
|
| 65 |
* If the user accesses http://example.com/?q=bar/baz, the menu system
|
| 66 |
* will find a match and execute page_example_baz().
|
| 67 |
*
|
| 68 |
* If the user accesses http://example.com/?q=bar/baz/1/2, the menu system
|
| 69 |
* will first look for bar/baz/1/2. Not finding a match, it will look for
|
| 70 |
* bar/baz/1/%. Again not finding a match, it will look for bar/baz/%/2. Yet
|
| 71 |
* again not finding a match, it will look for bar/baz/%/%. This time it finds
|
| 72 |
* a match, and so will execute page_example_baz(1, 2). Note the parameters
|
| 73 |
* being passed; this is a very useful technique.
|
| 74 |
*/
|
| 75 |
function page_example_menu() {
|
| 76 |
// This is the minimum information you can provide for a menu item.
|
| 77 |
$items['foo'] = array(
|
| 78 |
'title' => 'Foo',
|
| 79 |
'page callback' => 'page_example_foo',
|
| 80 |
'access arguments' => array('access foo'),
|
| 81 |
);
|
| 82 |
|
| 83 |
// By using the MENU_CALLBACK type, we can register the callback for this
|
| 84 |
// path but not have the item show up in the menu; the admin is not allowed
|
| 85 |
// to enable the item in the menu, either.
|
| 86 |
//
|
| 87 |
// Notice that the 'page arguments' is an array of numbers. These will be
|
| 88 |
// replaced with the corresponding parts of the menu path. In this case a 0
|
| 89 |
// would be replaced by 'bar', a 1 by 'baz', and like wise 2 and 3 will be
|
| 90 |
// replaced by what ever the user provides. These will be passed as arguments
|
| 91 |
// to the page_example_baz() function.
|
| 92 |
$items['bar/baz/%/%'] = array(
|
| 93 |
'title' => 'Baz',
|
| 94 |
'page callback' => 'page_example_baz',
|
| 95 |
'page arguments' => array(2, 3),
|
| 96 |
'access arguments' => array('access baz'),
|
| 97 |
'type' => MENU_CALLBACK,
|
| 98 |
);
|
| 99 |
|
| 100 |
return $items;
|
| 101 |
}
|
| 102 |
|
| 103 |
/**
|
| 104 |
* A simple page callback.
|
| 105 |
*
|
| 106 |
* Page callbacks are required to return the entire page. The content
|
| 107 |
* is then usually output via a call to theme('page'), where the theme system
|
| 108 |
* will then surround the content in the appropriate blocks, navigation, and
|
| 109 |
* styling.
|
| 110 |
*
|
| 111 |
* If you do not want to use the theme system (for example for outputting an
|
| 112 |
* image or XML), you should print the content yourself and not return anything.
|
| 113 |
*/
|
| 114 |
function page_example_foo() {
|
| 115 |
return '<p>'. t('The quick brown fox jumps over the lazy dog.') .'</p>';
|
| 116 |
}
|
| 117 |
|
| 118 |
/**
|
| 119 |
* A more complex page callback that takes arguments.
|
| 120 |
*
|
| 121 |
* The arguments are passed in from the page URL. The in our hook_menu
|
| 122 |
* implementation we instructed the menu system to extract the last two
|
| 123 |
* parameters of the path and pass them to this function as arguments.
|
| 124 |
*/
|
| 125 |
function page_example_baz($alice, $bob) {
|
| 126 |
// Make sure you don't trust the URL to be safe! Always check for exploits.
|
| 127 |
if (!is_numeric($alice) || !is_numeric($bob)) {
|
| 128 |
// We will just show a standard "access denied" page in this case.
|
| 129 |
return drupal_access_denied();
|
| 130 |
}
|
| 131 |
|
| 132 |
$list[] = t("Alice's number was @number.", array('@number' => $alice));
|
| 133 |
$list[] = t("Bob's number was @number.", array('@number' => $bob));
|
| 134 |
$list[] = t('The total was @number.', array('@number' => $alice + $bob));
|
| 135 |
|
| 136 |
return theme('item_list', $list);
|
| 137 |
}
|
| 138 |
|