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.)
Callback Functions
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',
);
return $items;
}
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.
Callback Arguments
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). To pass an integer without it being replaced with its respective path component, use the string value of the integer (e.g., '1') as the argument value. 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',
),
);
return $items;
}
When path 'abc/def' is requested, the page callback function will get 'def' as the first argument and (always) 'foo' as the second argument.
If a page callback function uses an argument list array, and its path is requested with optional path arguments, then the list array's arguments are passed to the callback function first, followed by the optional path arguments. Using the above example, when path 'abc/def/bar/baz' is requested, mymodule_abc_view() will be called with 'def', 'foo', 'bar' and 'baz' as arguments, in that order.
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 in Paths
Simple Wildcards
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. Note that wildcards may not be used as the first component.
Auto-Loader Wildcards
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 callback functions defined for the menu item, such as the page callback function mymodule_abc_edit() to replace the integer 1 in the argument array. Note that a load function should return FALSE when it is unable to provide a loadable object. For example, the node_load() function for the 'node/%node/edit' menu item will return FALSE for the path 'node/999/edit' if a node with a node ID of 999 does not exist. The menu routing system will return a 404 error in this case.
Argument Wildcards
You can also define a %wildcard_to_arg() function (for the example menu entry above this would be 'mymodule_abc_to_arg()'). The _to_arg() function is invoked to retrieve a value that is used in the path in place of the wildcard. A good example is user.module, which defines user_uid_optional_to_arg() (corresponding to the menu entry 'tracker/%user_uid_optional'). This function returns the user ID of the current user.
The _to_arg() function will get called with three arguments:
$arg: A string representing whatever argument may have been supplied by the caller (this is particularly useful if you want the _to_arg() function only supply a (default) value if no other value is specified, as in the case of user_uid_optional_to_arg().
$map: An array of all path fragments (e.g. array('node','123','edit') for 'node/123/edit').
$index: An integer indicating which element of $map corresponds to $arg.
_load() and _to_arg() functions may seem similar at first glance, but they have different purposes and are called at different times. _load() functions are called when the menu system is collecting arguments to pass to the callback functions defined for the menu item. _to_arg() functions are called when the menu system is generating links to related paths, such as the tabs for a set of MENU_LOCAL_TASK items.
Rendering Menu Items As Tabs
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/system/foo'] = array(
'title' => 'Foo settings',
'type' => MENU_NORMAL_ITEM,
);
// Make "Tab 1" the main tab on the "Foo settings" page
$items['admin/config/system/foo/tab1'] = array(
'title' => 'Tab 1',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Make an additional tab called "Tab 2" on "Foo settings"
$items['admin/config/system/foo/tab2'] = array(
'title' => 'Tab 2',
'type' => MENU_LOCAL_TASK,
);
Return value
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:
"title": Required. The untranslated title of the menu item.
"title callback": Function to generate the title; defaults to t(). If you require only the raw string to be output, set this to FALSE.
"title arguments": Arguments to send to t() or your custom callback, with path component substitution as described above.
"description": The untranslated description of the menu item.
"page callback": The function to call to display a web page when the user visits the path. If omitted, the parent menu item's callback will be used instead.
"page arguments": An array of arguments to pass to the page callback function, with path component substitution as described above.
"delivery callback": The function to call to package the result of the page callback function and send it to the browser. Defaults to drupal_deliver_html_page() unless a value is inherited from a parent menu item. Note that this function is called even if the access checks fail, so any custom delivery callback function should take that into account. See drupal_deliver_html_page() for an example.
"access callback": A function returning TRUE if the user has access rights to this menu item, and FALSE if not. It can also be a boolean constant instead of a function, and you can also use numeric values (will be cast to boolean). Defaults to user_access() unless a value is inherited from the parent menu item; only MENU_DEFAULT_LOCAL_TASK items can inherit access callbacks. To use the user_access() default callback, you must specify the permission to check as 'access arguments' (see below).
"access arguments": An array of arguments to pass to the access callback function, with path component substitution as described above. If the access callback is inherited (see above), the access arguments will be inherited with it, unless overridden in the child menu item.
"theme callback": (optional) A function returning the machine-readable name of the theme that will be used to render the page. If not provided, the value will be inherited from a parent menu item. If there is no theme callback, or if the function does not return the name of a current active theme on the site, the theme for this page will be determined by either hook_custom_theme() or the default theme instead. As a general rule, the use of theme callback functions should be limited to pages whose functionality is very closely tied to a particular theme, since they can only be overridden by modules which specifically target those pages in hook_menu_alter(). Modules implementing more generic theme switching functionality (for example, a module which allows the theme to be set dynamically based on the current user's role) should usehook_custom_theme() instead.
"theme arguments": An array of arguments to pass to the theme callback function, with path component substitution as described above.
"file": A file that will be included before the page callback is called; this allows page callback functions to be in separate files. The file should be relative to the implementing module's directory unless otherwise specified by the "file path" option. Does not apply to other callbacks (only page callback).
"file path": The path to the directory containing the file specified in "file". This defaults to the path to the module implementing the hook.
"load arguments": An array of arguments to be passed to each of the wildcard object loaders in the path, after the path argument itself. For example, if a module registers path node/%node/revisions/%/view with load arguments set to array(3), the '%node' in the path indicates that the loader function node_load() will be called with the second path component as the first argument. The 3 in the load arguments indicates that the fourth path component will also be passed to node_load() (numbering of path components starts at zero). So, if path node/12/revisions/29/view is requested, node_load(12, 29) will be called. There are also two "magic" values that can be used in load arguments. "%index" indicates the index of the wildcard path component. "%map" indicates the path components as an array. For example, if a module registers for several paths of the form 'user/%user_category/edit/*', all of them can use the same load function user_category_load(), by setting the load arguments to array('%map', '%index'). For instance, if the user is editing category 'foo' by requesting path 'user/32/edit/foo', the load function user_category_load() will be called with 32 as its first argument, the array ('user', 32, 'edit', 'foo') as the map argument, and 1 as the index argument (because %user_category is the second path component and numbering starts at zero). user_category_load() can then use these values to extract the information that 'foo' is the category being requested.
"weight": An integer that determines the relative position of items in the menu; higher-weighted items sink. Defaults to 0. Menu items with the same weight are ordered alphabetically.
"menu_name": Optional. Set this to a custom menu if you don't want your item to be placed in Navigation.
"expanded": Optional. If set to TRUE, and if a menu link is provided for this menu item (as a result of other properties), then the menu link is always expanded, equivalent to its 'always expanded' checkbox being set in the UI.
"context": (optional) Defines the context a tab may appear in. By default, all tabs are only displayed as local tasks when being rendered in a page context. All tabs that should be accessible as contextual links in page region containers outside of the parent menu item's primary page context should be registered using one of the following contexts:
Contexts can be combined. For example, to display a tab both on a page and inline, a menu router item may specify:
MENU_CONTEXT_PAGE: (default) The tab is displayed as local task for the page context only.
MENU_CONTEXT_INLINE: The tab is displayed as contextual link outside of the primary page context only.
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
"tab_parent": For local task menu items, the path of the task's parent item; defaults to the same path without the last component (e.g., the default parent for 'admin/people/create' is 'admin/people').
"tab_root": For local task menu items, the path of the closest non-tab item; same default as "tab_parent".
"position": Position of the block ('left' or 'right') on the system administration page for this item.
"type": A bitmask of flags describing properties of the menu item. Many shortcut bitmasks are provided as constants in menu.inc:
If the "type" element is omitted, MENU_NORMAL_ITEM is assumed.
MENU_NORMAL_ITEM: Normal menu items show up in the menu tree and can be moved/hidden by the administrator.
MENU_CALLBACK: Callbacks simply register a path so that the correct information is generated when the path is accessed.
MENU_SUGGESTED_ITEM: Modules may "suggest" menu items that the administrator may enable.
MENU_LOCAL_ACTION: Local actions are menu items that describe actions on the parent item such as adding a new user or block, and are rendered in the action-links list in your theme.
MENU_LOCAL_TASK: Local tasks are menu items that describe different displays of data, and are generally rendered as tabs.
MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one "default" task, which should display the same page as the parent item.
"options": An array of options to be passed to l() when generating a link from this menu item. Note that the "options" parameter has no effect on MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, and MENU_LOCAL_ACTION items.
For a detailed usage example, see page_example.module. For comprehensive documentation on the menu system, see http://drupal.org/node/102338.
Related topics
File
modules/
system/
system.api.php, line 1264
Hooks provided by Drupal core and the System module.
Code
function hook_menu() {
$items['example'] = array(
'title' => 'Example Page',
'page callback' => 'example_page',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['example/feed'] = array(
'title' => 'Example RSS feed',
'page callback' => 'example_feed',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
Comments
In your menu item
In your menu item definitions, you can specify TRUE as the access callback to make the item always accessible.
See http://api.drupal.org/api/function/_menu_check_access/7.
Log in or register to post comments
Some examples
The other day, I was trying to create a menu item with a dynamically generated path and hit a couple road blocks. After some reading I eventually worked it out and thought I would post an example here. It all came down to the wildcards and how to handle them. I was using a local task menu item as an example at first, but soon figured out that the wildcard technique for local tasks are quite a bit different than normal menu items...
These are all the readings I used to compile *some* understanding of the menu system:
Api Docs
Drupal Menu System Overview:
Dynamic Menu Argument replacement. This one is Good!
Wildcard usage and core's wildcards This one also has the full list of all the existing wildcard loaders for D6 (probably similar for D7)
http://api.drupal.org/api/function/page_example_menu
http://api.drupal.org/api/group/menu
Google Books:
Pro Drupal Development Chapter 4 - The Menu System
Log in or register to post comments
MENU_VISIBLE_IN_BREADCRUMB
In Drupal 6.x, menu items of type MENU_CALLBACK honor the title attribute. In 7.x, you'll find these items have a title like "Home". You can get something like the 6.x behavior by specifying type
MENU_VISIBLE_IN_BREADCRUMB
I thought this worth mentioning, even if it is already documented.
Log in or register to post comments
Show in Main Menu
If you want the menu item to show in the Main menu instead of the navigation, just set menu_name to 'main-menu', like so:
Log in or register to post comments
Parent item for nesting
Additionally, you can specify a 'plid' of the parent menu item if you don't want your menu item to appear at the root of the specified menu.
Log in or register to post comments
expanded
in page_example.module, there is an $items variable with 'expanded' => TRUE, which isn't mentioned on this page or in the module:
Log in or register to post comments
Read if you have problems with tabs not showing up.
Not in the code above a small comment line:
in the code for tabs:
If you are having trouble with tabs not showing up using the code above you may need to add the access callback or access arguments as it says above.
Log in or register to post comments
Make menu tab items show up in modal overlay or admin theme
If you are adding menu items that you would like to come up in a modal overlay, or in a different theme), you'll need to also implement hook_admin_paths().
This should only be required if you are using a path other than something that starts with "admin", as Drupal by default adds the path "admin/*" in system_admin_paths().
Log in or register to post comments
Problem with faulty implementation
If you (like me) happen to do a faulty implementation of hook_menu and your site crashes and gives only 404 not found for all pages, try this: "Page Not Found" error on ALL pages of Drupal 6 website
Log in or register to post comments
Do not use l() in descriptions
Warning: using l() in descriptions will cause an infinite loop in Drupal core 7.7 and later. Using url() is safe.
Log in or register to post comments
Workaround for infinite loop
This is because Drupal 7 now uses the theme registry to render links, where Drupal 6 renders them inline.
There is, however, a workaround. The documentation for l() states that edge cases can prevent theme initialization and force inline link rendering. Example:
This variable can in fact be used anywhere using l() causes an infinite loop due to an uninitialized theme registry.
Log in or register to post comments
Note that the include file is
Note that the include file is not only loaded for the page callback, but for the title callback as well.
Log in or register to post comments
Just a note: if you put the
Just a note: if you put the title callback in that include file, and it starts disappearing... it's probably because of some other module doing menu stuff in a hook_init() implementation.
Log in or register to post comments
Don't alter stuff in access callback
Menu Access callback function gets called multiple time and sometimes even if you are not accessing corresponding menu item. Thats why dont alter anything in access callback function, just check if user can access the menu. You can place all that code in page callback.
Log in or register to post comments
add menu block container in administrator configruation page
Add menu block container in administrator configuration page
http://drupalst.com/blog/add-menu-block-administrator-configuration-page
Log in or register to post comments
Note that the title should be
Note that the title should be *untranslated* meaning that you shouldn't use t() for the title text.
Log in or register to post comments
Controllers in Drupal, how to add?
There is a awesome 'controller' module: http://drupal.org/node/1389042
that allows to write something like this one without adding your rules in the hook_menu():
Log in or register to post comments
page callback and access callback
I was having trouble getting a menu item to display with the following code:
It was continuously denying me access to the specified path in addition to not displaying the item in the menu. I discovered that the reason behind this is if you do not specify a page callback, then the access callback will acquire a value of 0 in the database and thus render that path/menu item inaccessible.
Working code:
Log in or register to post comments
uid only works with MENU_LOCAL_TASK
From what I can determine, using a uid in a path to access a different users profiles or other user paths only works if the menu type is MENU_LOCAL_TASK. I tried MENU_NORMAL_ITEM to no avail.
This works
This doesn't work
This blog post contains a potential solution that works, but doesn't seem to work with path aliases. http://yuriybabenko.com/blog/using-user-id-arguments-in-drupal-menu-items
Log in or register to post comments
Changing title on pages
Hi, i was desperately searching for how to change the title on a custom page, thx to IRC someone told me to use: drupal_set_title:
http://api.drupal.org/api/drupal/includes!bootstrap.inc/function/drupal_...
Log in or register to post comments
title arguments needs to be serialized?
When changed 'title_callback' and 'title_arguments' in hook_menu_get_item_alter(), I had to serialize the array in 'title_arguments'.
Log in or register to post comments
Edit Example with wildcard automagically callback.
Posting this to help other people as I had a hard time figuring it out myself.
The wildcard name set in the $item array key (%edit_example) is used
to trigger a function with the same name with _load suffix(edit_example_load()) automagically and will send the argument as parameter to this function.
Use the load function to check if the item you want to edit exists.
If load function returns false the form function(edit_example_form()) in "page arguments" is not called and an "page not found" will appear instead.
If not return false the return value will be the third parameter in the form function.
The number after the first "page argument"(The second "page argument") is pointing at wildcard in $item array key (represented by number if splitted by / like this 0/1/2/3/4/5)), i have not figured out why yet.
Totally logic!!! :D
Log in or register to post comments
Thx a lot! It's very helpfull
Thx a lot! It's very helpfull for me. But also i had some warning
Notice: Undefined offset: 2 in _menu_translate()
and fixed like in Undefined offset: 2 in _menu_translateLog in or register to post comments
t
On a side note, you should NOT be using t() in your menu declarations. Menu automatically pushes title and description through t(), you'll end up with things being double-translated...
Log in or register to post comments
Some caveats about hook_menu()
A) The path must be valid for it to show up in the menu. I struggled for a long time in creating a placeholder like this:
and wondering why no entry would be created. Looks like there's a validation step when hook_menu() runs.
B) The wildcard MUST begin with a letter. I was trying to stick to a convention of having _modulename as the prefix for functions that aren't directly implementing or creating a hook, like this:
But drupal will silently fail on the %_foo wildcard without any indication otherwise.
C) God forbid you ever accidentally create an empty path
$items[''] =
or something that evaluates to an empty path$items[''] = /* for some reason this turns into an empty path */
You will never be able to get rid of this menu item once created, due to some NULL checks, no matter how many times you rebuild menu/clear cache/remove module/etc. the only fix in this situation is to manually find the menu entry in your sql database (table: menu_links i think) and deleting it.Log in or register to post comments
JSON
I use
drupal_json_output
as delivery callback. But this isn't normal practice for the system. Should I expect problems?Log in or register to post comments
In Drupal 7 the pagetitle
In Drupal 7 the pagetitle does not work for MENU_LOCAL_TASK and perhaps MENU_CALLBACK,
http://alastaira.wordpress.com/2011/01/18/drupal-7-x-hook_menu-not-displ...
The title of a page turns to "Home".
The workaround in this post is to use MENU_VISIBLE_IN_BREADCRUMB instead.
Log in or register to post comments
other solution
I had the same problem with MENU_DEFAULT_LOCAL_TASK and MENU_LOCAL_TASK items.
My solution was to add this line in the 'page callback' function:
function my_page_callback() {
drupal_set_title(t('My page title'), PASS_THROUGH);
...;
}
Log in or register to post comments
plid not necessarily picked up even with clearing the cache
Thanks for the plid reminder!
Looking at the menu entry in the menu-links table shows that despite clearing the cache this setting is not necessarily picked up. In fact, it only registered when I changed the name and title of my menu item and proceeded to clear the cache.
I could have just updated directly in the database table myself but I mention this as perhaps something's amiss in the way hook_menu is read and passed to the database table?
Log in or register to post comments
Per the doc:
Per the doc:
"options": An array of options to be passed to l() when generating a link from this menu item. Note that the "options" parameter has no effect on MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, and MENU_LOCAL_ACTION items.
I "should" be able to set "html" => true here and have l() not strip out the html. This does not work. My menu item is a MENU_NORMAL_ITEM so I don't see any reason for this not to work.
Tracing the code on a menu router rebuild, I see the property peristing through _menu_link_build. I do not see where this option is stored anywhere in the menu tables though. Options under menu_links just has the title attribute.
I put a breakpoint on every call to l() in menu.inc and I can see where my menu item is build, however I do not see where the options specified in the menu item are making it into the localized_options array that is passed into the l() call.
Is anyone else using this feature? I'm running 7.22.
Log in or register to post comments
NM
after a day and a half I found the issue... my options key had a pesky tab after it... so the issue was due to my fat fingers...
Log in or register to post comments
Delivery callback
The "delivery callback" function is very powerfull but it is not documented.
By default it calls the drupal_deliver_html_page() function, that returns a full rendered HTML page, including, header, footer, blocks, etc.
For webservices applications Drupal provides a json output using the ajax_deliver() function.
This function is very easy to manipulate, for example if you want your call to return just the content of your page without headers, footers and so, just use the following function:
This one could be used as response for jQuery.load().
Log in or register to post comments
Sub link of the not display
Here i have created the menu and sub link for the menu , it's created but it shows only menu, not a sub link of menu .
I want display the sub link of menu
function mymodule_coupon_menu() {
$menu_items['admin/mymodule'] = array(
'title' => 'mymodule',
'callback' => 'drupal_goto',
'page arguments' => array('admin/mymodule'),
'access arguments' => array('wandisco coupon'),
'weight' => 15,
'expanded' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
$menu_items['admin/mymodule/coupon'] = array(
'title' => t('mymodule coupon'),
'page callback' => 'drupal_get_form',
'page arguments' => array('admin/mymodule/coupon'),
'access arguments' => array('mymodule coupon'),
'weight' => 0,
'expanded' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $menu_items;
}
Log in or register to post comments
Page not found? look out with arg()
If you happen to call arg(X) within the callback function, and the argument is not set, a "Page not found" page will be displayed instead.
So when you're desperate after a few menu_rebuild()'s, flushing cache and maybe even rebuild via menu_rebuild(), just check your code for this.
Log in or register to post comments
RETURN of 'page callback'
From https://api.drupal.org/api/drupal/includes!common.inc/function/drupal_de...
Log in or register to post comments
...A renderable array of content
The structure of that renderable array is somewhat described here:
https://www.drupal.org/node/930760
Log in or register to post comments
Drupal 8 uses a new routing system
See https://drupal.org/node/1800686.
Log in or register to post comments
Undocumented: multiple types is possible
I wanted a menu item to show up both as MENU_LOCAL_TASK and MENU_NORMAL_ITEM. This can be achieved with a pipe delimiter! Like so:
'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
Log in or register to post comments
Create Menu tab while creating unique custom front page
i create a custom frontpage page--frontpage.tpl.php... Actually i want to create a menu tab dynamically.. i try this "
print drupal_render($main_menu_tree);
" but not work in custom page.. Please help me....Log in or register to post comments
explanation in spanish
Este modulo nos permite crear el contenido en una ruta, por ejemplo si la ruta es 'propiedad/la_mia', un ejemplo seria:
in the file:
standart.module
Log in or register to post comments
Restrict by User Role
This took me some to figure out so maybe it could help to someone else.
I wanted to allow access to the users from some Roles.
I has to write access arguments this way:
Then my validation function looks looks like this:
Log in or register to post comments
user_has_role
FWIW user_has_role is an existing function so don't call your function that. On the other hand, user_has_role () does return a boolean so can totally be used as an access function - but only for a single role ID. So you'd be able to do something like this:
Note use of the string value '1' per the documentation on Callback arguments above: "To pass an integer without it being replaced with its respective path component, use the string value of the integer (e.g., '1') as the argument value."
Log in or register to post comments
Typo
This look like a typo:
'mymodule_abc_to_arg()' should be '%mymodule_abc_to_arg()'
Log in or register to post comments
Nope
No, the "%wildcard" has been correctly replaced with the module name "mymodule". Function names cannot include punctuation.
If you do come across a typo in the docs please file an issue where it will receive the attention of the team responsible.
Log in or register to post comments
Cache clear when you arrange weight for each menu
You might can't get effect just set weight for menu and sub menu until you clear cache.
Log in or register to post comments
Do not use t() for menu title and description
Some of the examples above are using t() for title like 'title' => t('Title').
These will result in calling t() twice and you will end up with translated strings in the locales_source table because Drupal will call t(t('Title')).
Log in or register to post comments
maximum 9 elements for a menu callback !
https://api.drupal.org/api/drupal/includes!menu.inc/constant/MENU_MAX_PA...
Log in or register to post comments
In some cases, to avoid the
In some cases, to avoid the Object of class stdClass could not be converted to int , you might want to put 'page callback', 'page arguments', 'access callback', and 'access arguments' near the end of your item list. For example, this commit to the Nodequeue module was required to work around that notice.
Log in or register to post comments
Looks like the important
Looks like the important thing there is putting 'access callback' after 'page callback', and maybe 'access arguments' after 'access callback', but I could be wrong.
Log in or register to post comments
If your link doesn't appear
1. Clear cache
2. Make sure your hook_menu ends with a return:
return $items;
Log in or register to post comments
Pages
1
2
next ›
last »
function hook_menu
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.)
Callback Functions
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':
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.
Callback Arguments
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). To pass an integer without it being replaced with its respective path component, use the string value of the integer (e.g., '1') as the argument value. This substitution feature allows you to re-use a callback function for several different paths. For example:
When path 'abc/def' is requested, the page callback function will get 'def' as the first argument and (always) 'foo' as the second argument.
If a page callback function uses an argument list array, and its path is requested with optional path arguments, then the list array's arguments are passed to the callback function first, followed by the optional path arguments. Using the above example, when path 'abc/def/bar/baz' is requested, mymodule_abc_view() will be called with 'def', 'foo', 'bar' and 'baz' as arguments, in that order.
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:
See Form API documentation for details.
Wildcards in Paths
Simple Wildcards
Wildcards within paths also work with integer substitution. For example, your module could register path 'my-module/%/edit':
When path 'my-module/foo/edit' is requested, integer 1 will be replaced with 'foo' and passed to the callback function. Note that wildcards may not be used as the first component.
Auto-Loader Wildcards
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':
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:
This 'abc' object will then be passed into the callback functions defined for the menu item, such as the page callback function mymodule_abc_edit() to replace the integer 1 in the argument array. Note that a load function should return FALSE when it is unable to provide a loadable object. For example, the node_load() function for the 'node/%node/edit' menu item will return FALSE for the path 'node/999/edit' if a node with a node ID of 999 does not exist. The menu routing system will return a 404 error in this case.
Argument Wildcards
You can also define a %wildcard_to_arg() function (for the example menu entry above this would be 'mymodule_abc_to_arg()'). The _to_arg() function is invoked to retrieve a value that is used in the path in place of the wildcard. A good example is user.module, which defines user_uid_optional_to_arg() (corresponding to the menu entry 'tracker/%user_uid_optional'). This function returns the user ID of the current user.
The _to_arg() function will get called with three arguments:
_load() and _to_arg() functions may seem similar at first glance, but they have different purposes and are called at different times. _load() functions are called when the menu system is collecting arguments to pass to the callback functions defined for the menu item. _to_arg() functions are called when the menu system is generating links to related paths, such as the tabs for a set of MENU_LOCAL_TASK items.
Rendering Menu Items As Tabs
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:
Return value
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:
For a detailed usage example, see page_example.module. For comprehensive documentation on the menu system, see http://drupal.org/node/102338.
Related topics
File
Code
Comments
According to https://api
According to https://api.drupal.org/comment/56808#comment-56808, though it works and drupal incorporate this access-control but conceptually seems not a good solution, because default
access callback
out-of-box handles theuser_access
anduser_access
have$account
in signature which is a current login user, and every user assign to specific role/roles. So seems always better to use the permission attribute in the access arguments so we can manage the access-control-flow using the permission only & for that we do not need to use custom access callback.Note: Custom
access callback
is applicable if we need to control more granular level e.g. we want access control on top of$node
so we need to handlenode_access
and on that context it's helpful to write some access callback with custom logic usinguser_access & node_access
In this unlikely scenario:
In this unlikely scenario:
then visiting
example-name/example-name/test
will give you a "page not found" message. The two names cannot be the same.---
A module machine name (or module short name) can only contains the characters that are allowed in a PHP function name. You cannot use example-name as module machine name, since you cannot have a function whose name starts with example-name, which also means you cannot even implement hooks for that module.
good to include callback page to avoid white screen on url hit
Good if we include the callback function in the code and write some valid code, this will not let complete white screen and ascertain the code written is working. e.g.-
function custom_example_menu() {
$items = array();
$items['admin/config/system/custom-example'] = array(
'title' => 'Custom Example',
'page callback' => 'customexample_page',
'access callback' => TRUE,
);
return $items;
}
/*
* include following call back page
*/
function customexample_page() {
return 'Hi, this custom example config page and code is working!';
}
Log in or register to post comments
Log in or register to post comments
you have a sub-site installation with the sub-directory name "example-name"
you have a custom module with the same name (i.e. "example-name")
you define this
Log in or register to post comments
modules/
system/
system.api.php, line 1264
Hooks provided by Drupal core and the System module.
Hooks
Allow modules to interact with the Drupal core.
MENU_NORMAL_ITEM: Normal menu items show up in the menu tree and can be moved/hidden by the administrator.
MENU_CALLBACK: Callbacks simply register a path so that the correct information is generated when the path is accessed.
MENU_SUGGESTED_ITEM: Modules may "suggest" menu items that the administrator may enable.
MENU_LOCAL_ACTION: Local actions are menu items that describe actions on the parent item such as adding a new user or block, and are rendered in the action-links list in your theme.
MENU_LOCAL_TASK: Local tasks are menu items that describe different displays of data, and are generally rendered as tabs.
MENU_DEFAULT_LOCAL_TASK: Every set of local tasks should provide one "default" task, which should display the same page as the parent item.
"tab_parent": For local task menu items, the path of the task's parent item; defaults to the same path without the last component (e.g., the default parent for 'admin/people/create' is 'admin/people').
"tab_root": For local task menu items, the path of the closest non-tab item; same default as "tab_parent".
"position": Position of the block ('left' or 'right') on the system administration page for this item.
"type": A bitmask of flags describing properties of the menu item. Many shortcut bitmasks are provided as constants in menu.inc:
If the "type" element is omitted, MENU_NORMAL_ITEM is assumed.
"options": An array of options to be passed to l() when generating a link from this menu item. Note that the "options" parameter has no effect on MENU_LOCAL_TASK, MENU_DEFAULT_LOCAL_TASK, and MENU_LOCAL_ACTION items.
MENU_CONTEXT_PAGE: (default) The tab is displayed as local task for the page context only.
MENU_CONTEXT_INLINE: The tab is displayed as contextual link outside of the primary page context only.
"title": Required. The untranslated title of the menu item.
"title callback": Function to generate the title; defaults to t(). If you require only the raw string to be output, set this to FALSE.
"title arguments": Arguments to send to t() or your custom callback, with path component substitution as described above.
"description": The untranslated description of the menu item.
"page callback": The function to call to display a web page when the user visits the path. If omitted, the parent menu item's callback will be used instead.
"page arguments": An array of arguments to pass to the page callback function, with path component substitution as described above.
"delivery callback": The function to call to package the result of the page callback function and send it to the browser. Defaults to drupal_deliver_html_page() unless a value is inherited from a parent menu item. Note that this function is called even if the access checks fail, so any custom delivery callback function should take that into account. See drupal_deliver_html_page() for an example.
"access callback": A function returning TRUE if the user has access rights to this menu item, and FALSE if not. It can also be a boolean constant instead of a function, and you can also use numeric values (will be cast to boolean). Defaults to user_access() unless a value is inherited from the parent menu item; only MENU_DEFAULT_LOCAL_TASK items can inherit access callbacks. To use the user_access() default callback, you must specify the permission to check as 'access arguments' (see below).
"access arguments": An array of arguments to pass to the access callback function, with path component substitution as described above. If the access callback is inherited (see above), the access arguments will be inherited with it, unless overridden in the child menu item.
"theme callback": (optional) A function returning the machine-readable name of the theme that will be used to render the page. If not provided, the value will be inherited from a parent menu item. If there is no theme callback, or if the function does not return the name of a current active theme on the site, the theme for this page will be determined by either hook_custom_theme() or the default theme instead. As a general rule, the use of theme callback functions should be limited to pages whose functionality is very closely tied to a particular theme, since they can only be overridden by modules which specifically target those pages in hook_menu_alter(). Modules implementing more generic theme switching functionality (for example, a module which allows the theme to be set dynamically based on the current user's role) should usehook_custom_theme() instead.
"theme arguments": An array of arguments to pass to the theme callback function, with path component substitution as described above.
"file": A file that will be included before the page callback is called; this allows page callback functions to be in separate files. The file should be relative to the implementing module's directory unless otherwise specified by the "file path" option. Does not apply to other callbacks (only page callback).
"file path": The path to the directory containing the file specified in "file". This defaults to the path to the module implementing the hook.
"load arguments": An array of arguments to be passed to each of the wildcard object loaders in the path, after the path argument itself. For example, if a module registers path node/%node/revisions/%/view with load arguments set to array(3), the '%node' in the path indicates that the loader function node_load() will be called with the second path component as the first argument. The 3 in the load arguments indicates that the fourth path component will also be passed to node_load() (numbering of path components starts at zero). So, if path node/12/revisions/29/view is requested, node_load(12, 29) will be called. There are also two "magic" values that can be used in load arguments. "%index" indicates the index of the wildcard path component. "%map" indicates the path components as an array. For example, if a module registers for several paths of the form 'user/%user_category/edit/*', all of them can use the same load function user_category_load(), by setting the load arguments to array('%map', '%index'). For instance, if the user is editing category 'foo' by requesting path 'user/32/edit/foo', the load function user_category_load() will be called with 32 as its first argument, the array ('user', 32, 'edit', 'foo') as the map argument, and 1 as the index argument (because %user_category is the second path component and numbering starts at zero). user_category_load() can then use these values to extract the information that 'foo' is the category being requested.
"weight": An integer that determines the relative position of items in the menu; higher-weighted items sink. Defaults to 0. Menu items with the same weight are ordered alphabetically.
"menu_name": Optional. Set this to a custom menu if you don't want your item to be placed in Navigation.
"expanded": Optional. If set to TRUE, and if a menu link is provided for this menu item (as a result of other properties), then the menu link is always expanded, equivalent to its 'always expanded' checkbox being set in the UI.
"context": (optional) Defines the context a tab may appear in. By default, all tabs are only displayed as local tasks when being rendered in a page context. All tabs that should be accessible as contextual links in page region containers outside of the parent menu item's primary page context should be registered using one of the following contexts:
Contexts can be combined. For example, to display a tab both on a page and inline, a menu router item may specify:
$arg: A string representing whatever argument may have been supplied by the caller (this is particularly useful if you want the _to_arg() function only supply a (default) value if no other value is specified, as in the case of user_uid_optional_to_arg().
$map: An array of all path fragments (e.g. array('node','123','edit') for 'node/123/edit').
$index: An integer indicating which element of $map corresponds to $arg.
来自 https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu/7.x