hook_menu_contextual_links_alter

7 system.api.php hook_menu_contextual_links_alter(&$links, $router_item, $root_path)
8 system.api.php hook_menu_contextual_links_alter(&$links, $router_item, $root_path)

Alter contextual links before they are rendered.

This hook is invoked by menu_contextual_links(). The system-determined contextual links are passed in by reference. Additional links may be added or existing links can be altered.

Each contextual link must at least contain:

  • title: The localized title of the link.
  • href: The system path to link to.
  • localized_options: An array of options to pass to url().

Parameters

$links: An associative array containing contextual links for the given $root_path, 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.

$router_item: The menu router item belonging to the $root_path being requested.

$root_path: The (parent) path that has been requested to build contextual links for. This is a normalized path, which means that an originally passed path of 'node/123' became 'node/%'.

See also

hook_contextual_links_view_alter()

menu_contextual_links()

hook_menu()

contextual_preprocess()

Related topics

1 invocation of hook_menu_contextual_links_alter()

File

modules/system/system.api.php, line 1507
Hooks provided by Drupal core and the System module.

Code

function hook_menu_contextual_links_alter(&$links, $router_item, $root_path) {
  // Add a link to all contextual links for nodes.
  if ($root_path == 'node/%') {
    $links['foo'] = array(
      'title' => t('Do fu'), 
      'href' => 'foo/do', 
      'localized_options' => array(
        'query' => array(
          'foo' => 'bar',
        ),
      ),
    );
  }
}

Comments

Some tips

Thought I'd share a few tip I just found out.

Myself I wanted to have the translate node link appear in my contextual links menu and that was easy enough until I figured it was stupid to make db query calls etc to check if the node was translatable or not. This is a much nicer solution that you can use to alter other modules menu items to appear in the contextual links menu, just look for the the MODULE_menu() and check to see the what the item identifier is.

function HOOK_menu_alter(&$items) {
  $items['node/%node/translate']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
}

This hook will show the contextual links menu on full node displays as well.

function HOOK_node_view_alter(&$build) {
  if (!empty($build['#node']->nid)) {
    $build['#contextual_links']['node'] = array('node', array($build['#node']->nid));
  }
}

Login or register to post comments