You are here

function hook_menu_local_tasks_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/menu.api.php \hook_menu_local_tasks_alter()
  2. 7 modules/system/system.api.php \hook_menu_local_tasks_alter()

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.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability: The cacheability metadata for the current route's local tasks.

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().
1 invocation of hook_menu_local_tasks_alter()
LocalTaskManager::getLocalTasks in core/lib/Drupal/Core/Menu/LocalTaskManager.php
Renders the local tasks (tabs) for the given route.

File

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

Code

function hook_menu_local_tasks_alter(&$data, $route_name, \Drupal\Core\Cache\RefinableCacheableDependencyInterface &$cacheability) {

  // 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'),
        ],
      ],
    ],
  ];

  // The tab we're adding is dependent on a user's access to add content.
  $cacheability
    ->addCacheContexts([
    'user.permissions',
  ]);
}