LocalTaskManager::getTasksBuild

public LocalTaskManager::getTasksBuild($current_route_name, RefinableCacheableDependencyInterface &$cacheability)

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

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 = array();

  // Collect all route names.
  $route_names = array();
  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 $instances \Drupal\Core\Menu\LocalTaskInterface[] */
    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;
}
doc_Drupal
2016-10-29 09:24:50
Comments
Leave a Comment

Please login to continue.