function LocalActionManager::getActionsForRoute

Same name in other branches
  1. 9 core/lib/Drupal/Core/Menu/LocalActionManager.php \Drupal\Core\Menu\LocalActionManager::getActionsForRoute()
  2. 8.9.x core/lib/Drupal/Core/Menu/LocalActionManager.php \Drupal\Core\Menu\LocalActionManager::getActionsForRoute()
  3. 11.x core/lib/Drupal/Core/Menu/LocalActionManager.php \Drupal\Core\Menu\LocalActionManager::getActionsForRoute()

File

core/lib/Drupal/Core/Menu/LocalActionManager.php, line 168

Class

LocalActionManager
Provides the default local action manager using YML as primary definition.

Namespace

Drupal\Core\Menu

Code

public function getActionsForRoute($route_appears) {
    if (!isset($this->instances[$route_appears])) {
        $route_names = [];
        $this->instances[$route_appears] = [];
        // @todo Optimize this lookup by compiling or caching.
        foreach ($this->getDefinitions() as $plugin_id => $action_info) {
            if (in_array($route_appears, $action_info['appears_on'])) {
                $plugin = $this->createInstance($plugin_id);
                $route_names[] = $plugin->getRouteName();
                $this->instances[$route_appears][$plugin_id] = $plugin;
            }
        }
        // Pre-fetch all the action route objects. This reduces the number of SQL
        // queries that would otherwise be triggered by the access manager.
        if (!empty($route_names)) {
            $this->routeProvider
                ->getRoutesByNames($route_names);
        }
    }
    $links = [];
    $cacheability = new CacheableMetadata();
    $cacheability->addCacheContexts([
        'route',
    ]);
    
    /** @var \Drupal\Core\Menu\LocalActionInterface $plugin */
    foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
        $route_name = $plugin->getRouteName();
        $route_parameters = $plugin->getRouteParameters($this->routeMatch);
        $access = $this->accessManager
            ->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
        $links[$plugin_id] = [
            '#theme' => 'menu_local_action',
            '#link' => [
                'title' => $this->getTitle($plugin),
                'url' => Url::fromRoute($route_name, $route_parameters),
                'localized_options' => $plugin->getOptions($this->routeMatch),
            ],
            '#access' => $access,
            '#weight' => $plugin->getWeight(),
        ];
        $cacheability->addCacheableDependency($access)
            ->addCacheableDependency($plugin);
    }
    $cacheability->applyTo($links);
    return $links;
}

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