Define the navigation menus, local actions and tasks, and contextual links.
The menu system uses routes; see the Routing API topic for more information. It is used for navigation menus, local tasks, local actions, and contextual links:
The following sections of this topic provide an overview of the menu API. For more detailed information, see https://www.drupal.org/developing/api/8/menu
Routes for administrative tasks can be added to the main Drupal administrative menu hierarchy. To do this, add lines like the following to a module_name.links.menu.yml file (in the top-level directory for your module):
dblog.overview: title: 'Recent log messages' parent: system.admin_reports description: 'View events that have recently been logged.' route_name: dblog.overview weight: -1
Some notes:
Discovered menu links from other modules can be altered using hook_menu_links_discovered_alter().
@todo Derivatives will probably be defined for these; when they are, add documentation here.
Local tasks appear as tabs on a page when there are at least two defined for a route, including the base route as the main tab, and additional routes as other tabs. Static local tasks can be defined by adding lines like the following to a module_name.links.task.yml file (in the top-level directory for your module):
book.admin: route_name: book.admin title: 'List' base_route: book.admin book.settings: route_name: book.settings title: 'Settings' base_route: book.admin weight: 100
Some notes:
Local tasks from other modules can be altered using hook_menu_local_tasks_alter().
@todo Derivatives are in flux for these; when they are more stable, add documentation here.
Local actions can be defined for operations related to a given route. For instance, adding content is a common operation for the content management page, so it should be a local action. Static local actions can be defined by adding lines like the following to a module_name.links.action.yml file (in the top-level directory for your module):
node.add_page: route_name: node.add_page title: 'Add content' appears_on: - system.admin_content
Some notes:
Local actions from other modules can be altered using hook_menu_local_actions_alter().
@todo Derivatives are in flux for these; when they are more stable, add documentation here.
Contextual links are displayed by the Contextual Links module for user interface elements whose render arrays have a '#contextual_links' element defined. For example, a block render array might look like this, in part:
array( '#contextual_links' => array( 'block' => array( 'route_parameters' => array('block' => $entity->id()), ), ),
In this array, the outer key 'block' defines a "group" for contextual links, and the inner array provides values for the route's placeholder parameters (see @ref sec_placeholders above).
To declare that a defined route should be a contextual link for a contextual links group, put lines like the following in a module_name.links.contextual.yml file (in the top-level directory for your module):
block_configure: title: 'Configure block' route_name: 'entity.block.edit_form' group: 'block'
Some notes:
Contextual links from other modules can be altered using hook_contextual_links_alter().
@todo Derivatives are in flux for these; when they are more stable, add documentation here.
Once you have created menus (that contain menu links), you want to render them. Drupal provides a block (Drupal\system\Plugin\Block\SystemMenuBlock) to do so.
However, perhaps you have more advanced needs and you're not satisfied with what the menu blocks offer you. If that's the case, you'll want to:
Combined, that would look like this:
$menu_tree = \Drupal::menuTree(); $menu_name = 'my_menu'; // Build the typical default set of menu tree parameters. $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name); // Load the tree based on this set of parameters. $tree = $menu_tree->load($menu_name, $parameters); // Transform the tree using the manipulators you want. $manipulators = array( // Only show links that are accessible for the current user. array('callable' => 'menu.default_tree_manipulators:checkAccess'), // Use the default sorting of menu links. array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'), ); $tree = $menu_tree->transform($tree, $manipulators); // Finally, build a renderable array from the transformed tree. $menu = $menu_tree->build($tree); $menu_html = drupal_render($menu);
Name | Location | Description |
---|---|---|
hook_contextual_links_alter | core/lib/Drupal/Core/Menu/menu.api.php | Alter contextual links before they are rendered. |
hook_contextual_links_plugins_alter | core/lib/Drupal/Core/Menu/menu.api.php | Alter the plugin definition of contextual links. |
hook_local_tasks_alter | core/lib/Drupal/Core/Menu/menu.api.php | Alter local tasks plugins. |
hook_menu_links_discovered_alter | core/lib/Drupal/Core/Menu/menu.api.php | Alters all the menu links discovered by the menu link plugin manager. |
hook_menu_local_actions_alter | core/lib/Drupal/Core/Menu/menu.api.php | Alter local actions plugins. |
hook_menu_local_tasks_alter | core/lib/Drupal/Core/Menu/menu.api.php | Alter local tasks displayed on the page before they are rendered. |
hook_system_breadcrumb_alter | core/lib/Drupal/Core/Menu/menu.api.php | Perform alterations to the breadcrumb built by the BreadcrumbManager. |
menu_cache_clear_all | core/includes/menu.inc | Clears all cached menu data. |
menu_list_system_menus | core/includes/menu.inc | Returns an array containing the names of system-defined (default) menus. |
menu_local_tabs | core/includes/menu.inc | Returns a renderable element for the primary and secondary tabs. |
menu_local_tasks Deprecated | core/includes/menu.inc | Collects the local tasks (tabs) for the current route. |
menu_primary_local_tasks Deprecated | core/includes/menu.inc | Returns the rendered local tasks at the top level. |
menu_secondary_local_tasks Deprecated | core/includes/menu.inc | Returns the rendered local tasks at the second level. |
template_preprocess_menu_local_action | core/includes/menu.inc | Prepares variables for single local action link templates. |
template_preprocess_menu_local_task | core/includes/menu.inc | Prepares variables for single local task link templates. |
Name | Location | Description |
---|---|---|
ContextualLinkInterface | core/lib/Drupal/Core/Menu/ContextualLinkInterface.php | Defines a contextual link plugin. |
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Menu!menu.api.php/group/menu/8.1.x