8.5.x menu.inc template_preprocess_menu_local_task(&$variables)
8.0.x menu.inc template_preprocess_menu_local_task(&$variables)
8.1.x menu.inc template_preprocess_menu_local_task(&$variables)
8.2.x menu.inc template_preprocess_menu_local_task(&$variables)
8.3.x menu.inc template_preprocess_menu_local_task(&$variables)
8.4.x menu.inc template_preprocess_menu_local_task(&$variables)
8.6.x menu.inc template_preprocess_menu_local_task(&$variables)

Prepares variables for single local task link templates.

Default template: menu-local-task.html.twig.

Parameters

array $variables: An associative array containing:

  • element: A render element containing:

    • #link: A menu link array with 'title', 'url', and (optionally) 'localized_options' keys.
    • #active: A boolean indicating whether the local task is active.

Related topics

File

core/includes/menu.inc, line 28
API for the Drupal menu system.

Code

function template_preprocess_menu_local_task(&$variables) {
  $link = $variables['element']['#link'];
  $link += [
    'localized_options' => [],
  ];
  $link_text = $link['title'];
  if (!empty($variables['element']['#active'])) {
    $variables['is_active'] = TRUE;

    // Add text to indicate active tab for non-visual users.
    $active = SafeMarkup::format('<span class="visually-hidden">@label</span>', [
      '@label' => t('(active tab)'),
    ]);
    $link_text = t('@local-task-title@active', [
      '@local-task-title' => $link_text,
      '@active' => $active,
    ]);
  }
  $link['localized_options']['set_active_class'] = TRUE;
  $variables['link'] = [
    '#type' => 'link',
    '#title' => $link_text,
    '#url' => $link['url'],
    '#options' => $link['localized_options'],
  ];
}