You are here

public function LocalTaskManager::getTasksBuild in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/LocalTaskManager.php \Drupal\Core\Menu\LocalTaskManager::getTasksBuild()

Gets the render array for all local tasks.

Parameters

string $current_route_name: The route for which to make renderable local tasks.

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $cacheability: The cacheability metadata for the local tasks.

Return value

array A render array as expected by menu-local-tasks.html.twig.

Overrides LocalTaskManagerInterface::getTasksBuild

1 call to LocalTaskManager::getTasksBuild()
LocalTaskManager::getLocalTasks in core/lib/Drupal/Core/Menu/LocalTaskManager.php
Renders the local tasks (tabs) for the given route.

File

core/lib/Drupal/Core/Menu/LocalTaskManager.php, line 289

Class

LocalTaskManager
Provides the default local task manager using YML as primary definition.

Namespace

Drupal\Core\Menu

Code

public function getTasksBuild($current_route_name, RefinableCacheableDependencyInterface &$cacheability) {
  $tree = $this
    ->getLocalTasksForRoute($current_route_name);
  $build = [];

  // Collect all route names.
  $route_names = [];
  foreach ($tree as $instances) {
    foreach ($instances as $child) {
      $route_names[] = $child
        ->getRouteName();
    }
  }

  // Pre-fetch all routes involved in the tree. This reduces the number
  // of SQL queries that would otherwise be triggered by the access manager.
  if ($route_names) {
    $this->routeProvider
      ->getRoutesByNames($route_names);
  }
  foreach ($tree as $level => $instances) {

    /** @var \Drupal\Core\Menu\LocalTaskInterface[] $instances */
    foreach ($instances as $plugin_id => $child) {
      $route_name = $child
        ->getRouteName();
      $route_parameters = $child
        ->getRouteParameters($this->routeMatch);

      // Given that the active flag depends on the route we have to add the
      // route cache context.
      $cacheability
        ->addCacheContexts([
        'route',
      ]);
      $active = $this
        ->isRouteActive($current_route_name, $route_name, $route_parameters);

      // The plugin may have been set active in getLocalTasksForRoute() if
      // one of its child tabs is the active tab.
      $active = $active || $child
        ->getActive();

      // @todo It might make sense to use link render elements instead.
      $link = [
        'title' => $this
          ->getTitle($child),
        'url' => Url::fromRoute($route_name, $route_parameters),
        'localized_options' => $child
          ->getOptions($this->routeMatch),
      ];
      $access = $this->accessManager
        ->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
      $build[$level][$plugin_id] = [
        '#theme' => 'menu_local_task',
        '#link' => $link,
        '#active' => $active,
        '#weight' => $child
          ->getWeight(),
        '#access' => $access,
      ];
      $cacheability
        ->addCacheableDependency($access)
        ->addCacheableDependency($child);
    }
  }
  return $build;
}