function NavigationRenderer::getLocalTasks

Same name and namespace in other branches
  1. 10 core/modules/navigation/src/NavigationRenderer.php \Drupal\navigation\NavigationRenderer::getLocalTasks()

Local tasks list based on user access.

Return value

array Local tasks keyed by route name.

1 call to NavigationRenderer::getLocalTasks()
NavigationRenderer::hasLocalTasks in core/modules/navigation/src/NavigationRenderer.php
Do we have local tasks that we want to show in the top bar?

File

core/modules/navigation/src/NavigationRenderer.php, line 246

Class

NavigationRenderer
Handle rendering for different pieces of the navigation.

Namespace

Drupal\navigation

Code

public function getLocalTasks() : array {
  if (isset($this->localTasks)) {
    return $this->localTasks;
  }
  $cacheability = new CacheableMetadata();
  $cacheability->addCacheableDependency($this->localTaskManager);
  $this->localTasks = [
    'page_actions' => [],
    'cacheability' => $cacheability,
  ];
  // For now, we're only interested in local tasks corresponding to a content
  // entity.
  if (!$this->entityRouteHelper
    ->isContentEntityRoute()) {
    return $this->localTasks;
  }
  $entity_local_tasks = $this->localTaskManager
    ->getLocalTasks($this->routeMatch
    ->getRouteName());
  uasort($entity_local_tasks['tabs'], [
    SortArray::class,
    'sortByWeightProperty',
  ]);
  foreach ($entity_local_tasks['tabs'] as $local_task_name => $local_task) {
    // Exclude current route local task, since it is not going to be included
    // in the page actions link list.
    $url = $local_task['#link']['url'] ?? NULL;
    if ($url?->getRouteName() === $entity_local_tasks['route_name']) {
      continue;
    }
    // The $local_task array that we get here is tailor-made for use
    // with the menu-local-tasks.html.twig, eg. the menu_local_task
    // theme hook. It has all the information we need, but we're not
    // rendering local tasks, or tabs, we're rendering a simple list of
    // links. Here we're taking advantage of all the good stuff found in
    // the render array, namely the #link, and #access properties, using
    // them to render a simple link.
    // @see \Drupal\Core\Menu\LocalTaskManager::getTasksBuild()
    $link = $local_task['#link'];
    $link['localized_options'] += [
      'set_active_class' => TRUE,
    ];
    $this->localTasks['page_actions'][$local_task_name] = [
      '#theme' => 'top_bar_page_action',
      '#link' => [
        '#type' => 'link',
        '#title' => $link['title'],
        '#url' => $link['url'],
        '#options' => $link['localized_options'],
      ],
      '#access' => $local_task['#access'],
    ];
  }
  $this->localTasks['cacheability'] = $cacheability->merge($entity_local_tasks['cacheability']);
  return $this->localTasks;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.