You are here

public function LocalTasksBlock::build in Drupal 8

Same name and namespace in other branches
  1. 9 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()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

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;
}