Menu system

Define the navigation menus, and route page requests to code based on URLs.

The Drupal menu system drives both the navigation system from a user perspective and the callback system that Drupal 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 Drupal administrative interface.

Drupal'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
  • 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 Drupal 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

drupal/core/includes/menu.inc, line 18
API for the Drupal menu system.

Functions

Namesort ascending Location Description
_menu_tree_data drupal/core/includes/menu.inc Builds the data representing a menu tree.
_menu_tree_check_access drupal/core/includes/menu.inc Sorts the menu tree and recursively checks access for each item.
_menu_translate drupal/core/includes/menu.inc Handles dynamic path translation and menu access control.
_menu_site_is_offline drupal/core/includes/menu.inc Checks whether the site is in maintenance mode.
_menu_set_expanded_menus drupal/core/includes/menu.inc Updates a list of menus with expanded items.
_menu_router_translate_route drupal/core/includes/menu.inc Translates a route name to its router item path.
_menu_router_save drupal/core/includes/menu.inc Saves data from menu_router_build() to the router table.
_menu_router_cache drupal/core/includes/menu.inc Stores the menu router if we have it in memory.
_menu_router_build drupal/core/includes/menu.inc Builds the router table based on the data from hook_menu().
_menu_navigation_links_rebuild drupal/core/includes/menu.inc Builds menu links for the items in the menu router.
_menu_load_objects drupal/core/includes/menu.inc Loads objects into the map as defined in the $item['load_functions'].
_menu_link_translate drupal/core/includes/menu.inc Provides menu link access control, translation, and argument handling.
_menu_link_map_translate drupal/core/includes/menu.inc Translates the path elements in the map using any to_arg helper function.
_menu_item_localize drupal/core/includes/menu.inc Localizes the router item title using t() or another callback.
_menu_find_router_path drupal/core/includes/menu.inc Finds the router path which will serve this path.
_menu_clear_page_cache drupal/core/includes/menu.inc Clears the page and block caches at most twice per page load.
_menu_check_access drupal/core/includes/menu.inc Checks access to a menu item using the access callback.
_menu_build_tree drupal/core/includes/menu.inc Builds a menu tree.
theme_menu_tree drupal/core/includes/menu.inc Returns HTML for a wrapper for a menu sub-tree.
theme_menu_local_tasks drupal/core/includes/menu.inc Returns HTML for primary and secondary local tasks.
theme_menu_local_task drupal/core/includes/menu.inc Returns HTML for a single local task link.
theme_menu_local_action drupal/core/includes/menu.inc Returns HTML for a single local action link.
theme_menu_link drupal/core/includes/menu.inc Returns HTML for a menu link and submenu.
template_preprocess_menu_tree drupal/core/includes/menu.inc Implements template_preprocess_HOOK() for theme_menu_tree().
menu_unserialize drupal/core/includes/menu.inc Unserializes menu data, using a map to replace path elements.
menu_tree_set_path drupal/core/includes/menu.inc Sets the path for determining the active trail of the specified menu tree.
menu_tree_page_data drupal/core/includes/menu.inc Gets the data structure for a named menu tree, based on the current page.
menu_tree_output drupal/core/includes/menu.inc Returns a rendered menu tree.
menu_tree_get_path drupal/core/includes/menu.inc Gets the path for determining the active trail of the specified menu tree.
menu_tree_data drupal/core/includes/menu.inc Sorts and returns the built data representing a menu tree.
menu_tree_collect_node_links drupal/core/includes/menu.inc Collects node links from a given menu tree recursively.
menu_tree_check_access drupal/core/includes/menu.inc Checks access and performs dynamic operations for each link in the tree.
menu_tree_all_data drupal/core/includes/menu.inc Gets the data structure representing a named menu tree.
menu_tree drupal/core/includes/menu.inc Renders a menu tree based on the current path.
menu_tail_to_arg drupal/core/includes/menu.inc Returns a string containing the path relative to the current index.
menu_tail_load drupal/core/includes/menu.inc Loads the path as one string relative to the current index.
menu_tab_root_path drupal/core/includes/menu.inc Returns the router path, or the path for a default local task's parent.
menu_set_item drupal/core/includes/menu.inc Replaces the statically cached item for a given path.
menu_set_custom_theme drupal/core/includes/menu.inc Sets a custom theme for the current page, if there is one.
menu_set_active_trail drupal/core/includes/menu.inc Sets the active trail (path to the menu tree root) of the current page.
menu_set_active_menu_names drupal/core/includes/menu.inc Sets (or gets) the active menu for the current page.
menu_set_active_item drupal/core/includes/menu.inc Sets the active path, which determines which page is loaded.
menu_secondary_menu drupal/core/includes/menu.inc Returns an array of links to be rendered as the Secondary links.
menu_secondary_local_tasks drupal/core/includes/menu.inc Returns the rendered local tasks at the second level.
menu_router_rebuild drupal/core/includes/menu.inc Populates the database tables used by various menu functions.
menu_router_build drupal/core/includes/menu.inc Collects and alters the menu definitions.
menu_reset_static_cache drupal/core/includes/menu.inc Resets the menu system static cache.
menu_primary_local_tasks drupal/core/includes/menu.inc Returns the rendered local tasks at the top level.
menu_navigation_links drupal/core/includes/menu.inc Returns an array of links for a navigation menu.
menu_main_menu drupal/core/includes/menu.inc Returns an array of links to be rendered as the Main menu.
menu_local_tasks drupal/core/includes/menu.inc Collects the local tasks (tabs), action links, and the root path.
menu_local_tabs drupal/core/includes/menu.inc Returns a renderable element for the primary and secondary tabs.
menu_load_links drupal/core/includes/menu.inc Returns an array containing all links for a menu.
menu_list_system_menus drupal/core/includes/menu.inc Returns an array containing the names of system-defined (default) menus.
menu_link_get_preferred drupal/core/includes/menu.inc Looks up the preferred menu link for a given system path.
menu_item_route_access drupal/core/includes/menu.inc Checks access to a menu item by mocking a request for a path.
menu_get_router drupal/core/includes/menu.inc Gets the menu router.
menu_get_object drupal/core/includes/menu.inc Gets a loaded object from a router item.
menu_get_local_actions drupal/core/includes/menu.inc Returns the rendered local actions at the current level.
menu_get_item drupal/core/includes/menu.inc Gets a router item.
menu_get_custom_theme drupal/core/includes/menu.inc Gets the custom theme for the current page, if there is one.
menu_get_ancestors drupal/core/includes/menu.inc Returns the ancestors (and relevant placeholders) for any given path.
menu_get_active_trail drupal/core/includes/menu.inc Gets the active trail (path to root menu root) of the current page.
menu_get_active_title drupal/core/includes/menu.inc Gets the title of the current page, as determined by the active trail.
menu_get_active_menu_names drupal/core/includes/menu.inc Gets the active menu for the current page.
menu_get_active_help drupal/core/includes/menu.inc Returns the help associated with the active menu item.
menu_get_active_breadcrumb drupal/core/includes/menu.inc Gets the breadcrumb for the current page, as determined by the active trail.
menu_delete_links drupal/core/includes/menu.inc Deletes all links for a menu.
menu_contextual_links drupal/core/includes/menu.inc Retrieves contextual links for a path based on registered local tasks.
menu_cache_clear_all drupal/core/includes/menu.inc Clears all cached menu data.
menu_cache_clear drupal/core/includes/menu.inc Clears the cached cached data for a single named menu.
menu_build_tree drupal/core/includes/menu.inc Builds a menu tree, translates links, and checks access.
drupal_help_arg drupal/core/includes/menu.inc Generates elements for the $arg array in the help hook.

Constants

Namesort ascending Location Description
MENU_VISIBLE_IN_TREE drupal/core/includes/menu.inc Internal menu flag -- menu item is visible in the menu tree.
MENU_VISIBLE_IN_BREADCRUMB drupal/core/includes/menu.inc Internal menu flag -- menu item is visible in the breadcrumb.
MENU_SUGGESTED_ITEM drupal/core/includes/menu.inc Menu type -- A normal menu item, hidden until enabled by an administrator.
MENU_SITE_ONLINE drupal/core/includes/menu.inc Internal menu status code -- Everything is working fine.
MENU_SITE_OFFLINE drupal/core/includes/menu.inc Internal menu status code -- Menu item inaccessible because site is offline.
MENU_SIBLING_LOCAL_TASK drupal/core/includes/menu.inc Menu type -- A task specific to the parent, which is never rendered.
MENU_PREFERRED_LINK drupal/core/includes/menu.inc Reserved key to identify the most specific menu link for a given path.
MENU_NOT_FOUND drupal/core/includes/menu.inc Internal menu status code -- Menu item was not found.
MENU_NORMAL_ITEM drupal/core/includes/menu.inc Menu type -- A "normal" menu item that's shown in menu and breadcrumbs.
MENU_MODIFIED_BY_ADMIN drupal/core/includes/menu.inc Internal menu flag -- menu item can be modified by administrator.
MENU_MAX_PARTS drupal/core/includes/menu.inc The maximum number of path elements for a menu callback
MENU_MAX_DEPTH drupal/core/includes/menu.inc The maximum depth of a menu links tree - matches the number of p columns.
MENU_LOCAL_TASK drupal/core/includes/menu.inc Menu type -- A task specific to the parent item, usually rendered as a tab.
MENU_LOCAL_ACTION drupal/core/includes/menu.inc Menu type -- An action specific to the parent, usually rendered as a link.
MENU_LINKS_TO_PARENT drupal/core/includes/menu.inc Internal menu flag -- menu item links back to its parent.
MENU_IS_ROOT drupal/core/includes/menu.inc Internal menu flag -- menu item is the root of the menu tree.
MENU_IS_LOCAL_TASK drupal/core/includes/menu.inc Internal menu flag -- menu item is a local task.
MENU_IS_LOCAL_ACTION drupal/core/includes/menu.inc Internal menu flag -- menu item is a local action.
MENU_DEFAULT_LOCAL_TASK drupal/core/includes/menu.inc Menu type -- The "default" local task, which is initially active.
MENU_CREATED_BY_ADMIN drupal/core/includes/menu.inc Internal menu flag -- menu item was created by administrator.
MENU_CONTEXT_PAGE drupal/core/includes/menu.inc Internal menu flag: Local task should be displayed in page context.
MENU_CONTEXT_NONE drupal/core/includes/menu.inc Internal menu flag: Invisible local task.
MENU_CONTEXT_INLINE drupal/core/includes/menu.inc Internal menu flag: Local task should be displayed inline.
MENU_CALLBACK drupal/core/includes/menu.inc Menu type -- A hidden, internal callback, typically used for API calls.
MENU_ACCESS_DENIED drupal/core/includes/menu.inc Internal menu status code -- Menu item access is denied.

Sub-Topics

Namesort ascending Location Description
Menu tree parameters drupal/core/includes/menu.inc Parameters for a menu tree.
Menu status codes drupal/core/includes/menu.inc Status codes for menu callbacks.
Menu item types drupal/core/includes/menu.inc Definitions for various menu item types.
Menu flags drupal/core/includes/menu.inc Flags for use in the "type" attribute of menu items.
Menu context types drupal/core/includes/menu.inc Flags for use in the "context" attribute of menu router items.