8.5.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)
8.2.x menu.api.php hook_menu_local_tasks_alter(&$data, $route_name)
8.3.x menu.api.php hook_menu_local_tasks_alter(&$data, $route_name)
8.4.x menu.api.php hook_menu_local_tasks_alter(&$data, $route_name)
8.6.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 local tasks displayed on the page before they are rendered.

This hook is invoked by \Drupal\Core\Menu\LocalTaskManager::getLocalTasks(). The system-determined tabs and actions are passed in by reference. Additional tabs may be added.

The local tasks are under the 'tabs' element and keyed by plugin ID.

Each local task is an associative array containing:

  • #theme: The theme function to use to render.
  • #link: An associative array containing:
  • #weight: The link's weight compared to other links.
  • #active: Whether the link should be marked as 'active'.

Parameters

array $data: An associative array containing list of (up to 2) tab levels that contain a list of tabs keyed by their href, each one being an associative array as described above.

string $route_name: The route name of the page.

Related topics

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

contact_menu_local_tasks_alter in core/modules/contact/contact.module
Implements hook_menu_local_tasks_alter().
menu_test_menu_local_tasks_alter in core/modules/system/tests/modules/menu_test/menu_test.module
Implements hook_menu_local_tasks_alter().

File

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

Code

function hook_menu_local_tasks_alter(&$data, $route_name) {

  // Add a tab linking to node/add to all pages.
  $data['tabs'][0]['node.add_page'] = [
    '#theme' => 'menu_local_task',
    '#link' => [
      'title' => t('Example tab'),
      'url' => Url::fromRoute('node.add_page'),
      'localized_options' => [
        'attributes' => [
          'title' => t('Add content'),
        ],
      ],
    ],
  ];
}

Comments

Gogowitsch’s picture

To make sure this hook is called again when its conditions change, you can attach cache tags to the task definition. If you use YAML for this, here is an example:

zielwert.kollektivbildung_form:
  route_name: zielwert.kollektivbildung_form
  base_route: zielwert.zielwert_form
  title: 'Kollektivbildung'
  cache_tags:
  - durchgang_list

In this example: as soon as the durchgang_list cache tag is invalidated, the hook will be executed again. You could call Cache::invalidateTags(['durchgang_list']); to force this to happen.