/Drupal 8

function hook_contextual_links_alter

hook_contextual_links_alter(array &$links, $group, array $route_parameters)

Alter contextual links before they are rendered.

This hook is invoked by \Drupal\Core\Menu\ContextualLinkManager::getContextualLinkPluginsByGroup(). The system-determined contextual links are passed in by reference. Additional links may be added and existing links can be altered.

Each contextual link contains the following entries:

  • title: The localized title of the link.
  • route_name: The route name of the link.
  • route_parameters: The route parameters of the link.
  • localized_options: An array of URL options.
  • (optional) weight: The weight of the link, which is used to sort the links.


array $links: An associative array containing contextual links for the given $group, as described above. The array keys are used to build CSS class names for contextual links and must therefore be unique for each set of contextual links.

string $group: The group of contextual links being rendered.

array $route_parameters.: The route parameters passed to each route_name of the contextual links. For example:

  array('node' => $node->id())

See also


Related topics

Define functions that alter the behavior of Drupal core.
Menu system
Define the navigation menus, local actions and tasks, and contextual links.


core/lib/Drupal/Core/Menu/menu.api.php, line 388
Hooks and documentation related to the menu system and links.


function hook_contextual_links_alter(array &$links, $group, array $route_parameters) {
  if ($group == 'menu') {
    // Dynamically use the menu name for the title of the menu_edit contextual
    // link.
    $menu = \Drupal::entityManager()->getStorage('menu')->load($route_parameters['menu']);
    $links['menu_edit']['title'] = t('Edit menu: @label', array('@label' => $menu->label()));

© 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.