function LocalTasksBlock::build

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php \Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::build()
  2. 10 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 85

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);
    $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.