function LocalTasksBlock::build

Same name in other branches
  1. 9 core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php \Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::build()
  2. 8.9.x core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php \Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::build()
  3. 11.x core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php \Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::build()

Overrides BlockPluginInterface::build

File

core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php, line 87

Class

LocalTasksBlock
Provides a "Tabs" block to display the local tasks.

Namespace

Drupal\Core\Menu\Plugin\Block

Code

public function build() {
    $config = $this->configuration;
    $cacheability = new CacheableMetadata();
    $cacheability->addCacheableDependency($this->localTaskManager);
    // If the current route belongs to an entity, include cache tags of that
    // entity as well.
    $route_parameters = $this->routeMatch
        ->getParameters()
        ->all();
    foreach ($route_parameters as $parameter) {
        if ($parameter instanceof CacheableDependencyInterface) {
            $cacheability->addCacheableDependency($parameter);
        }
    }
    $tabs = [
        '#theme' => 'menu_local_tasks',
    ];
    // Add only selected levels for the printed output.
    if ($config['primary']) {
        $links = $this->localTaskManager
            ->getLocalTasks($this->routeMatch
            ->getRouteName(), 0);
        $cacheability = $cacheability->merge($links['cacheability']);
        // Do not display single tabs.
        $tabs += [
            '#primary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
        ];
    }
    if ($config['secondary']) {
        $links = $this->localTaskManager
            ->getLocalTasks($this->routeMatch
            ->getRouteName(), 1);
        $cacheability = $cacheability->merge($links['cacheability']);
        // Do not display single tabs.
        $tabs += [
            '#secondary' => count(Element::getVisibleChildren($links['tabs'])) > 1 ? $links['tabs'] : [],
        ];
    }
    $build = [];
    $cacheability->applyTo($build);
    if (empty($tabs['#primary']) && empty($tabs['#secondary'])) {
        return $build;
    }
    return $build + $tabs;
}

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