Define the navigation menus, and route page requests to code based on URLs.
The Backdrop menu system drives both the navigation system from a user perspective and the callback system that Backdrop uses to respond to URLs passed from the browser. For this reason, a good understanding of the menu system is fundamental to the creation of complex modules. As a note, this is related to, but separate from menu.module, which allows menus (which in this context are hierarchical lists of links) to be customized from the Backdrop administrative interface.
Backdrop's menu system follows a simple hierarchy defined by paths. Implementations of hook_menu() define menu items and assign them to paths (which should be unique). The menu system aggregates these items and determines the menu hierarchy from the paths. For example, if the paths defined were a, a/b, e, a/b/c/d, f/g, and a/b/h, the menu system would form the structure:
- a
- a/b
- a/b/c/d
- a/b/h
- a/b
- e
- f/g
Note that the number of elements in the path does not necessarily determine the depth of the menu item in the tree.
When responding to a page request, the menu system looks to see if the path requested by the browser is registered as a menu item with a callback. If not, the system searches up the menu tree for the most complete match with a callback it can find. If the path a/b/i is requested in the tree above, the callback for a/b would be used.
The found callback function is called with any arguments specified in the "page arguments" attribute of its menu item. The attribute must be an array. After these arguments, any remaining components of the path are appended as further arguments. In this way, the callback for a/b above could respond to a request for a/b/i differently than a request for a/b/j.
For an illustration of this process, see page_example.module.
Access to the callback functions is also protected by the menu system. The "access callback" with an optional "access arguments" of each menu item is called before the page callback proceeds. If this returns TRUE, then access is granted; if FALSE, then access is denied. Default local task menu items (see next paragraph) may omit this attribute to use the value provided by the parent item.
In the default Backdrop interface, you will notice many links rendered as tabs. These are known in the menu system as "local tasks", and they are rendered as tabs by default, though other presentations are possible. Local tasks function just as other menu items in most respects. It is convention that the names of these tasks should be short verbs if possible. In addition, a "default" local task should be provided for each set. When visiting a local task's parent menu item, the default local task will be rendered as if it is selected; this provides for a normal tab user experience. This default task is special in that it links not to its provided path, but to its parent item's path instead. The default task's path is only used to place it appropriately in the menu hierarchy.
Everything described so far is stored in the menu_router table. The menu_links table holds the visible menu links. By default these are derived from the same hook_menu definitions, however you are free to add more with menu_link_save().
File
- core/
includes/ menu.inc, line 7 - API for the Backdrop menu system.
Functions
Name | Location | Description |
---|---|---|
menu_build_tree |
core/ |
Builds a menu tree, translates links, and checks access. |
menu_cache_clear |
core/ |
Clears the cached data for a single named menu. |
menu_cache_clear_all |
core/ |
Clears all cached menu data. |
menu_contextual_links |
core/ |
Retrieves contextual links for a path based on registered local tasks. |
menu_default_route_handler |
core/ |
Executes the current router item's page callback. |
menu_delete_links |
core/ |
Deletes all links for a menu. |
menu_execute_active_handler |
core/ |
Execute the page callback associated with the current path. |
menu_get_active_breadcrumb |
core/ |
Gets the breadcrumb for the current page, as determined by the active trail. |
menu_get_active_menu_names |
core/ |
Gets the active menu for the current page. |
menu_get_active_title |
core/ |
Gets the title of the current page, as determined by the active trail. |
menu_get_active_trail |
core/ |
Gets the active trail (path to root menu root) of the current page. |
menu_get_ancestors |
core/ |
Returns the ancestors (and relevant placeholders) for any given path. |
menu_get_custom_theme |
core/ |
Gets the custom theme for the current page, if there is one. |
menu_get_item |
core/ |
Gets a router item. |
menu_get_object |
core/ |
Gets a loaded object from a router item. |
menu_get_router |
core/ |
Gets the menu router. |
menu_links_clone |
core/ |
Clones an array of menu links. |
menu_link_children_relative_depth |
core/ |
Finds the depth of an item's children relative to its depth. |
menu_link_delete |
core/ |
Delete one or several menu links. |
menu_link_get_preferred |
core/ |
Looks up the preferred menu link for a given system path. |
menu_link_load |
core/ |
Gets a translated menu link that is ready for rendering. |
menu_link_maintain |
core/ |
Inserts, updates, or deletes an un-customized menu link related to a module. |
menu_link_save |
core/ |
Saves a menu link. |
menu_list_system_menus |
core/ |
Returns an array containing the names of system-defined (default) menus. |
menu_load_links |
core/ |
Returns an array containing all links for a menu. |
menu_local_actions |
core/ |
Returns the rendered local actions at the current level. |
menu_local_tabs |
core/ |
Returns rendered HTML for the primary and secondary tabs. |
menu_local_tasks |
core/ |
Collects the local tasks (tabs), action links, and the root path. |
menu_navigation_links |
core/ |
Returns an array of links for a navigation menu. |
menu_primary_local_tasks |
core/ |
Returns the rendered local tasks at the top level. |
menu_rebuild |
core/ |
Populates the database tables used by various menu functions. |
menu_reset_static_cache |
core/ |
Resets the menu system static cache. |
menu_router_build |
core/ |
Collects and alters the menu definitions. |
menu_secondary_local_tasks |
core/ |
Returns the rendered local tasks at the second level. |
menu_set_active_item |
core/ |
Sets the active path, which determines which page is loaded. |
menu_set_active_menu_names |
core/ |
Sets (or gets) the active menu for the current page. |
menu_set_active_trail |
core/ |
Sets the active trail (path to the menu tree root) of the current page. |
menu_set_custom_theme |
core/ |
Sets a custom theme for the current page, if there is one. |
menu_set_item |
core/ |
Replaces the statically cached item for a given path. |
menu_tab_root_path |
core/ |
Returns the router path, or the path for a default local task's parent. |
menu_tail_load |
core/ |
Loads the path as one string relative to the current index. |
menu_tail_to_arg |
core/ |
Returns a string containing the path relative to the current index. |
menu_tree |
core/ |
Renders a menu tree based on the current path. |
menu_tree_all_data |
core/ |
Gets the data structure representing a named menu tree. |
menu_tree_check_access |
core/ |
Checks access and performs dynamic operations for each link in the tree. |
menu_tree_collect_node_links |
core/ |
Collects node links from a given menu tree recursively. |
menu_tree_data |
core/ |
Sorts and returns the built data representing a menu tree. |
menu_tree_get_path |
core/ |
Gets the path for determining the active trail of the specified menu tree. |
menu_tree_output |
core/ |
Returns an output structure for rendering a menu tree. |
menu_tree_page_data |
core/ |
Gets the data structure for a named menu tree, based on the current page. |
menu_tree_set_path |
core/ |
Sets the path for determining the active trail of the specified menu tree. |
menu_unserialize |
core/ |
Unserializes menu data, using a map to replace path elements. |
template_preprocess_menu_tree |
core/ |
Implements template_preprocess_HOOK() for theme_menu_tree(). |
theme_menu_link |
core/ |
Returns HTML for a menu link and submenu. |
theme_menu_local_action |
core/ |
Returns HTML for a single local action link. |
theme_menu_local_actions |
core/ |
Returns rendered HTML for the local actions. |
theme_menu_local_task |
core/ |
Returns HTML for a single local task link. |
theme_menu_local_tasks |
core/ |
Returns HTML for primary and secondary local tasks. |
theme_menu_toggle |
core/ |
Returns rendered HTML for a menu toggle. |
theme_menu_tree |
core/ |
Returns HTML for a wrapper for a menu tree. |
_menu_build_tree |
core/ |
Builds a menu tree. |
_menu_check_access |
core/ |
Checks access to a menu item using the access callback. |
_menu_clear_page_cache |
core/ |
Clears the page and block caches at most twice per page load. |
_menu_delete_item |
core/ |
Deletes a single menu link. |
_menu_find_router_path |
core/ |
Finds the router path which will serve this path. |
_menu_item_localize |
core/ |
Localizes the router item title using t() or another callback. |
_menu_link_build |
core/ |
Builds a link from a router item. |
_menu_link_find_parent |
core/ |
Finds a possible parent for a given menu link. |
_menu_link_map_translate |
core/ |
Translates the path elements in the map using any to_arg helper function. |
_menu_link_move_children |
core/ |
Updates the children of a menu link that is being moved. |
_menu_link_parents_set |
core/ |
Sets the p1 through p9 values for a menu link being saved. |
_menu_link_translate |
core/ |
Provides menu link access control, translation, and argument handling. |
_menu_load_objects |
core/ |
Loads objects into the map as defined in the $item['load_functions']. |
_menu_navigation_links_rebuild |
core/ |
Builds menu links for the items in the menu router. |
_menu_router_build |
core/ |
Builds the router table based on the data from hook_menu(). |
_menu_router_cache |
core/ |
Stores the menu router if we have it in memory. |
_menu_router_save |
core/ |
Saves data from menu_router_build() to the router table. |
_menu_set_expanded_menus |
core/ |
Updates a list of menus with expanded items. |
_menu_site_status |
core/ |
Checks whether the site is in maintenance mode. |
_menu_translate |
core/ |
Handles dynamic path translation and menu access control. |
_menu_tree_check_access |
core/ |
Sorts the menu tree and recursively checks access for each item. |
_menu_tree_data |
core/ |
Builds the data representing a menu tree. |
_menu_update_parental_status |
core/ |
Checks and updates the 'has_children' status for the parent of a link. |
Constants
Name | Location | Description |
---|---|---|
MENU_ACCESS_DENIED |
core/ |
Menu status code -- Access denied. |
MENU_CALLBACK |
core/ |
Menu type -- A hidden, internal callback, typically used for API calls. |
MENU_CONTEXT_INLINE |
core/ |
Internal menu flag: Local task should be displayed inline. |
MENU_CONTEXT_NONE |
core/ |
Internal menu flag: Invisible local task. |
MENU_CONTEXT_PAGE |
core/ |
Internal menu flag: Local task should be displayed in page context. |
MENU_CREATED_BY_ADMIN |
core/ |
Internal menu flag -- menu item was created by administrator. |
MENU_DEFAULT_LOCAL_TASK |
core/ |
Menu type -- The "default" local task, which is initially active. |
MENU_IS_LOCAL_ACTION |
core/ |
Internal menu flag -- menu item is a local action. |
MENU_IS_LOCAL_TASK |
core/ |
Internal menu flag -- menu item is a local task. |
MENU_IS_ROOT |
core/ |
Internal menu flag -- menu item is the root of the menu tree. |
MENU_LINKS_TO_PARENT |
core/ |
Internal menu flag -- menu item links back to its parent. |
MENU_LOCAL_ACTION |
core/ |
Menu type -- An action specific to the parent, usually rendered as a link. |
MENU_LOCAL_TASK |
core/ |
Menu type -- A task specific to the parent item, usually rendered as a tab. |
MENU_MAX_DEPTH |
core/ |
The maximum depth of a menu links tree - matches the number of p columns. |
MENU_MAX_PARTS |
core/ |
The maximum number of path elements for a menu callback |
MENU_MODIFIED_BY_ADMIN |
core/ |
Internal menu flag -- menu item can be modified by administrator. |
MENU_NORMAL_ITEM |
core/ |
Menu type -- A "normal" menu item that's shown in menu and breadcrumbs. |
MENU_NOT_FOUND |
core/ |
Menu status code -- Not found. |
MENU_PREFERRED_LINK |
core/ |
Reserved key to identify the most specific menu link for a given path. |
MENU_SITE_OFFLINE |
core/ |
Internal menu status code -- Menu item inaccessible because site is offline. |
MENU_SITE_ONLINE |
core/ |
Internal menu status code -- Everything is working fine. |
MENU_SUGGESTED_ITEM |
core/ |
Menu type -- A normal menu item, hidden until enabled by an administrator. |
MENU_VISIBLE_IN_BREADCRUMB |
core/ |
Internal menu flag -- menu item is visible in the breadcrumb. |
MENU_VISIBLE_IN_TREE |
core/ |
Internal menu flag -- menu item is visible in the menu tree. |
Sub-Topics
Name | Location | Description |
---|---|---|
Menu context types |
core/ |
Flags for use in the "context" attribute of menu router items. |
Menu flags |
core/ |
Flags for use in the "type" attribute of menu items. |
Menu item types |
core/ |
Definitions for various menu item types. |
Menu status codes |
core/ |
Status codes for menu callbacks. |
Menu tree parameters |
core/ |
Parameters for a menu tree. |