You are here

public function LocalTasksBlock::build in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php \Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::build()
  2. 9 core/lib/Drupal/Core/Menu/Plugin/Block/LocalTasksBlock.php \Drupal\Core\Menu\Plugin\Block\LocalTasksBlock::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;
}