hook_menu()
drupal/modules/system/system.api.php, line 989
Define menu items and page callbacks.
This hook enables modules to register paths in order to define how URL requests are handled. Paths may be registered for URL handling only, or they can register a link to be placed in a menu (usually the Navigation menu). A path and its associated information is commonly called a "menu router item". This hook is rarely called (for example, when modules are enabled), and its results are cached in the database.
hook_menu() implementations return an associative array whose keys define paths and whose values are an associative array of properties for each path. (The complete list of properties is in the return value section below.)
The definition for each path may include a page callback function, which is invoked when the registered path is requested. If there is no other registered path that fits the requested path better, any further path components are passed to the callback function. For example, your module could register path 'abc/def':
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
);
}
function mymodule_abc_view($ghi = 0, $jkl = '') {
// ...
}
When path 'abc/def' is requested, no further path components are in the request, and no additional arguments are passed to the callback function (so $ghi and $jkl would take the default values as defined in the function signature). When 'abc/def/123/foo' is requested, $ghi will be '123' and $jkl will be 'foo'. Note that this automatic passing of optional path arguments applies only to page and theme callback functions.
In addition to optional path arguments, the page callback and other callback functions may specify argument lists as arrays. These argument lists may contain both fixed/hard-coded argument values and integers that correspond to path components. When integers are used and the callback function is called, the corresponding path components will be substituted for the integers. That is, the integer 0 in an argument list will be replaced with the first path component, integer 1 with the second, and so on (path components are numbered starting from zero). This substitution feature allows you to re-use a callback function for several different paths. For example:
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
);
}
When path 'abc/def' is requested, the page callback function will get 'def' as the first argument and (always) 'foo' as the second argument.
Note that if a page or theme callback function has an argument list array, these arguments will be passed first to the function, followed by any any arguments generated by optional path arguments as described above.
Special care should be taken for the page callback drupal_get_form(), because your specific form callback function will always receive $form and &$form_state as the first function arguments:
function mymodule_abc_form($form, &$form_state) {
// ...
return $form;
}
See Form API documentation for details.
Wildcards within paths also work with integer substitution. For example, your module could register path 'my-module/%/edit':
$items['my-module/%/edit'] = array(
'page callback' => 'mymodule_abc_edit',
'page arguments' => array(1),
);
When path 'my-module/foo/edit' is requested, integer 1 will be replaced with 'foo' and passed to the callback function.
Registered paths may also contain special "auto-loader" wildcard components in the form of '%mymodule_abc', where the '%' part means that this path component is a wildcard, and the 'mymodule_abc' part defines the prefix for a load function, which here would be named mymodule_abc_load(). When a matching path is requested, your load function will receive as its first argument the path component in the position of the wildcard; load functions may also be passed additional arguments (see "load arguments" in the return value section below). For example, your module could register path 'my-module/%mymodule_abc/edit':
$items['my-module/%mymodule_abc/edit'] = array(
'page callback' => 'mymodule_abc_edit',
'page arguments' => array(1),
);
When path 'my-module/123/edit' is requested, your load function mymodule_abc_load() will be invoked with the argument '123', and should load and return an "abc" object with internal id 123:
function mymodule_abc_load($abc_id) {
return db_query("SELECT * FROM {mymodule_abc} WHERE abc_id = :abc_id", array(':abc_id' => $abc_id))->fetchObject();
}
This 'abc' object will then be passed into the page callback function mymodule_abc_edit() to replace the integer 1 in the page arguments.
You can also make groups of menu items to be rendered (by default) as tabs on a page. To do that, first create one menu item of type MENU_NORMAL_ITEM, with your chosen path, such as 'foo'. Then duplicate that menu item, using a subdirectory path, such as 'foo/tab1', and changing the type to MENU_DEFAULT_LOCAL_TASK to make it the default tab for the group. Then add the additional tab items, with paths such as "foo/tab2" etc., with type MENU_LOCAL_TASK. Example:
// Make "Foo settings" appear on the admin Config page
$items['admin/config/foo'] = array(
'title' => 'Foo settings',
'type' => MENU_NORMAL_ITEM,
// page callback, etc. need to be added here
);
// Make "Global settings" the main tab on the "Foo settings" page
$items['admin/config/foo/global'] = array(
'title' => 'Global settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
// access callback, page callback, and theme callback will be inherited
// from 'admin/config/foo', if not specified here to override
);
// Make an additional tab called "Node settings" on "Foo settings"
$items['admin/config/foo/node'] = array(
'title' => 'Node settings',
'type' => MENU_LOCAL_TASK,
// access callback, page callback, and theme callback will be inherited
// from 'admin/config/foo', if not specified here to override
);
For a detailed usage example, see page_example.module. For comprehensive documentation on the menu system, see http://drupal.org/node/102338.
An array of menu items. Each menu item has a key corresponding to the Drupal path being registered. The corresponding array value is an associative array that may contain the following key-value pairs:
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
| Name | Description |
|---|---|
| Hooks | Allow modules to interact with the Drupal core. |
function hook_menu() {
$items['blog'] = array(
'title' => 'blogs',
'page callback' => 'blog_page',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['blog/feed'] = array(
'title' => 'RSS feed',
'page callback' => 'blog_feed',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}