function NavigationRenderer::getLocalTasks

Same name in other branches
  1. 11.x 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.

2 calls to NavigationRenderer::getLocalTasks()
NavigationRenderer::buildTopBar in core/modules/navigation/src/NavigationRenderer.php
Build the top bar for content entity pages.
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 235

Class

NavigationRenderer
Handle rendering for different pieces of the navigation.

Namespace

Drupal\navigation

Code

private function getLocalTasks() : array {
    if (isset($this->localTasks)) {
        return $this->localTasks;
    }
    $cacheability = new CacheableMetadata();
    $cacheability->addCacheableDependency($this->localTaskManager);
    $this->localTasks = [
        'tasks' => [],
        'cacheability' => $cacheability,
    ];
    // For now, we're only interested in local tasks corresponding to a content
    // entity.
    if (!$this->meetsContentEntityRoutesCondition()) {
        return $this->localTasks;
    }
    $entity_local_tasks = $this->localTaskManager
        ->getLocalTasks($this->routeMatch
        ->getRouteName());
    foreach ($entity_local_tasks['tabs'] as $route_name => $local_task) {
        // 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['tasks'][$route_name] = [
            '#theme' => 'top_bar_local_task',
            '#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.