8.2.x menu.api.php hook_menu_local_tasks_alter(&$data, $route_name)
8.0.x menu.api.php hook_menu_local_tasks_alter(&$data, $route_name)
8.1.x menu.api.php hook_menu_local_tasks_alter(&$data, $route_name)
7.x system.api.php hook_menu_local_tasks_alter(&$data, $router_item, $root_path)

Alter tabs and actions displayed on the page before they are rendered.

This hook is invoked by menu_local_tasks(). The system-determined tabs and actions are passed in by reference. Additional tabs or actions may be added, or existing items altered.

Each tab or action is an associative array containing:

  • #theme: The theme function to use to render.
  • #link: An associative array containing:
    • title: The localized title of the link.
    • href: The system path to link to.
    • localized_options: An array of options to pass to l().
  • #active: Whether the link should be marked as 'active'.

Parameters

$data: An associative array containing:

  • actions: An associative array containing:

    • count: The amount of actions determined by the menu system, which can be ignored.
    • output: A list of of actions, each one being an associative array as described above.
  • tabs: An indexed array (list) of tab levels (up to 2 levels), each containing an associative array:

    • count: The amount of tabs determined by the menu system. This value does not need to be altered if there is more than one tab.
    • output: A list of of tabs, each one being an associative array as described above.

$router_item: The menu system router item of the page.

$root_path: The path to the root item for this set of tabs.

Related topics

3 functions implement hook_menu_local_tasks_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

blog_menu_local_tasks_alter in modules/blog/blog.module
Implements hook_menu_local_tasks_alter().
forum_menu_local_tasks_alter in modules/forum/forum.module
Implements hook_menu_local_tasks_alter().
node_menu_local_tasks_alter in modules/node/node.module
Implements hook_menu_local_tasks_alter().
1 invocation of hook_menu_local_tasks_alter()
menu_local_tasks in includes/menu.inc
Collects the local tasks (tabs), action links, and the root path.

File

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

Code

function hook_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  // Add an action linking to node/add to all pages.
  $data['actions']['output'][] = array(
    '#theme' => 'menu_local_task',
    '#link' => array(
      'title' => t('Add new content'),
      'href' => 'node/add',
      'localized_options' => array(
        'attributes' => array(
          'title' => t('Add new content'),
        ),
      ),
    ),
  );

  // Add a tab linking to node/add to all pages.
  $data['tabs'][0]['output'][] = array(
    '#theme' => 'menu_local_task',
    '#link' => array(
      'title' => t('Example tab'),
      'href' => 'node/add',
      'localized_options' => array(
        'attributes' => array(
          'title' => t('Add new content'),
        ),
      ),
    ),
    // Define whether this link is active. This can be omitted for
    // implementations that add links to pages outside of the current page
    // context.
    '#active' => ($router_item['path'] == $root_path),
  );
}

Comments

prajaankit’s picture

I face the same problem, I see that the user module , how to comes login, create new a/c, request new pass word,
it is come in alphabetical order, i used menu alter, menu_local_tasks_alter, not applicable to change the weight
i search on drupal.org i find the module https://www.drupal.org/project/tabtamer

which solve me problem

teedave’s picture

I struggled to figure this out so thought I'd post here in case others want to do this.
Above it says:
Each tab or action is an associative array containing:

#theme: The theme function to use to render.
#link: An associative array containing:
title: The localized title of the link.
href: The system path to link to.
localized_options: An array of options to pass to l().
#active: Whether the link should be marked as 'active'.

But I found that I had to add "path" to the #link associative array to get it working e.g.

<?php
$tabs[0]['#theme'] = 'menu_local_task';
$tabs[0]['#link'] = array(
  'title' => 'Articles and events', 
  'href' => 'search/site/' . arg(2), 
  'path' => 'search/site/' . arg(2), 
  'localized_options' => array(
    'html' => TRUE
  )
);
$tabs[0]['#active'] = TRUE/FALSE;
?>

A trap for weary players...

deepcore’s picture

Hi guys,

I found a way to set a weight for local task items. I think this should be mentioned somewhere in documentation so people don't have to spend hours on looking for the solution.

/**
* Implements hook_menu_local_tasks_alter()
*
* @param $data
* @param $router_item
* @param $root_path
*/
function HOOK_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'admin/content') {
// You add #weight here on item level not link level
$data['tabs'][0]['output'][0]['#weight'] = '1000';
}
}

You can check it if it works for you.

mjt772’s picture

If anyone needs/wants a more dynamic approach (for instance, if you want to be able to modify a particular tab regardless of its level), here's a quick snippet to crawl the $data array and change (in my use case) the title of a local task link depending on its path:

<?php

function HOOK_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  foreach ($data['tabs'] as $key1 => $tabs) {
    foreach ($tabs['output'] as $key2 => $item) {
      // Conditionally change link title
      if ($item['#link']['path'] == 'some/path/here') {
        $data['tabs'][$key1]['output'][$key2]['#link']['title'] = 'New Title';
      }
    }
  }
}

?>
zamigr’s picture

Hi everyone,

Is it possible to change a default page actions? E.g. how to alter default "Add content" link in admin/content page? $data['actions']['output'] is empty by default.

brentgees’s picture

I had the problem off the local tasks not showing, even if I had more then 2 actions.

The solution was to update the $data['tabs']['output'] to a number higher then 1 (it's showing depending on this number)