function LocalTaskManager::getLocalTasks

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Menu/LocalTaskManager.php \Drupal\Core\Menu\LocalTaskManager::getLocalTasks()
  2. 10 core/lib/Drupal/Core/Menu/LocalTaskManager.php \Drupal\Core\Menu\LocalTaskManager::getLocalTasks()
  3. 9 core/lib/Drupal/Core/Menu/LocalTaskManager.php \Drupal\Core\Menu\LocalTaskManager::getLocalTasks()
  4. 8.9.x core/lib/Drupal/Core/Menu/LocalTaskManager.php \Drupal\Core\Menu\LocalTaskManager::getLocalTasks()

Renders the local tasks (tabs) for the given route.

Parameters

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

int $level: The level of tasks you ask for. Primary tasks are 0, secondary are 1.

Return value

array An array containing

  • tabs: Local tasks render array for the requested level.
  • route_name: The route name for the current page used to collect the local tasks.

Overrides LocalTaskManagerInterface::getLocalTasks

File

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

Class

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

Namespace

Drupal\Core\Menu

Code

public function getLocalTasks($route_name, $level = 0) {
  if ($this->loadingLocalTasks && \Fiber::getCurrent() !== NULL) {
    // Primary and secondary task are rendered in separate blocks, each within
    // their own fiber. Both call this method for a different level, but the
    // data is built for both levels on the first call. If the first call
    // gets suspended, for example due to an entity load in a URL access
    // check, the second block will then call into this. If the data is
    // already being built, and we're in a fiber, suspend once to allow the
    // first fiber to complete building the data. If it is still not done,
    // proceed anyway, which may build that information twice but will not
    // return incomplete local task data.
    \Fiber::suspend();
  }
  if (!isset($this->taskData[$route_name])) {
    $this->loadingLocalTasks = TRUE;
    $cacheability = new CacheableMetadata();
    $cacheability->addCacheContexts([
      'route',
    ]);
    // Look for route-based tabs.
    if (!$this->requestStack
      ->getCurrentRequest()->attributes
      ->has('exception')) {
      // Safe to build tasks only when no exceptions raised.
      $data = [];
      $local_tasks = $this->getTasksBuild($route_name, $cacheability);
      foreach ($local_tasks as $tab_level => $items) {
        $data[$tab_level] = empty($data[$tab_level]) ? $items : array_merge($data[$tab_level], $items);
      }
      $this->taskData[$route_name] = [
        'tabs' => $data,
        'cacheability' => $cacheability,
      ];
      // Allow modules to alter local tasks.
      $this->moduleHandler
        ->alter('menu_local_tasks', $this->taskData[$route_name], $route_name, $cacheability);
      $this->taskData[$route_name]['cacheability'] = $cacheability;
    }
    else {
      $this->taskData[$route_name] = [
        'tabs' => [],
        'cacheability' => $cacheability,
      ];
    }
    $this->loadingLocalTasks = FALSE;
  }
  if (isset($this->taskData[$route_name]['tabs'][$level])) {
    return [
      'tabs' => $this->taskData[$route_name]['tabs'][$level],
      'route_name' => $route_name,
      'cacheability' => $this->taskData[$route_name]['cacheability'],
    ];
  }
  return [
    'tabs' => [],
    'route_name' => $route_name,
    'cacheability' => $this->taskData[$route_name]['cacheability'],
  ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.