LocalActionManager.php
Same filename and directory in other branches
Namespace
Drupal\Core\MenuFile
-
core/
lib/ Drupal/ Core/ Menu/ LocalActionManager.php
View source
<?php
namespace Drupal\Core\Menu;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Cache\CacheCollectorInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlCacheCollectorDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Provides the default local action manager using YML as primary definition.
*/
class LocalActionManager extends DefaultPluginManager implements LocalActionManagerInterface {
/**
* Provides some default values for all local action plugins.
*
* @var array
*/
protected $defaults = [
// The plugin id. Set by the plugin system based on the top-level YAML key.
'id' => NULL,
// The static title for the local action.
'title' => '',
// The weight of the local action.
'weight' => NULL,
// (Required) the route name used to generate a link.
'route_name' => NULL,
// Default route parameters for generating links.
'route_parameters' => [],
// Associative array of link options.
'options' => [],
// The route names where this local action appears.
'appears_on' => [],
// Default class for local action implementations.
'class' => 'Drupal\\Core\\Menu\\LocalActionDefault',
];
/**
* The YAML cache collector.
*/
protected CacheCollectorInterface $yamlCacheCollector;
/**
* The plugin instances.
*
* @var \Drupal\Core\Menu\LocalActionInterface[]
*/
protected $instances = [];
public function __construct(protected ArgumentResolverInterface $argumentResolver, protected RequestStack $requestStack, protected RouteMatchInterface $routeMatch, protected RouteProviderInterface $routeProvider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, protected LanguageManagerInterface $languageManager, protected AccessManagerInterface $accessManager, protected AccountInterface $account, ?CacheCollectorInterface $yaml_cache_collector = NULL) {
if (!isset($yaml_cache_collector)) {
@trigger_error('Calling ' . __METHOD__ . '() without the $yamlCacheCollector argument is deprecated in drupal:11.5.0 and it will be required in drupal:12.0.0. See https://www.drupal.org/project/drupal/issues/3593485', E_USER_DEPRECATED);
$yaml_cache_collector = \Drupal::service('local_action.yaml_cache_collector');
}
$this->yamlCacheCollector = $yaml_cache_collector;
// Skip calling the parent constructor, since that assumes annotation-based
// discovery.
$this->moduleHandler = $module_handler;
$this->factory = new ContainerFactory($this, 'Drupal\\Core\\Menu\\LocalActionInterface');
$this->alterInfo('menu_local_actions');
$this->setCacheBackend($cache_backend, 'local_action_plugins:' . $this->languageManager
->getCurrentLanguage()
->getId());
}
/**
* {@inheritdoc}
*/
protected function getDiscovery() {
if (!isset($this->discovery)) {
$yaml_discovery = new YamlCacheCollectorDiscovery('links.action', $this->moduleHandler
->getModuleDirectories(), $this->yamlCacheCollector);
$yaml_discovery->addTranslatableProperty('title', 'title_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($yaml_discovery);
}
return $this->discovery;
}
/**
* {@inheritdoc}
*/
public function getTitle(LocalActionInterface $local_action) {
$controller = [
$local_action,
'getTitle',
];
$arguments = $this->argumentResolver
->getArguments($this->requestStack
->getCurrentRequest(), $controller);
return call_user_func_array($controller, $arguments);
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
$cids = [];
foreach ($this->languageManager
->getLanguages() as $language) {
$cids[] = 'local_action_plugins:' . $language->getId();
}
$this->cacheBackend
->deleteMultiple($cids);
$this->definitions = NULL;
}
}
Classes
| Title | Deprecated | Summary |
|---|---|---|
| LocalActionManager | Provides the default local action manager using YML as primary definition. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.