3 namespace Drupal\Core\Menu;
5 use Drupal\Core\Access\AccessManagerInterface;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Cache\CacheBackendInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Plugin\DefaultPluginManager;
11 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
12 use Drupal\Core\Plugin\Discovery\YamlDiscovery;
13 use Drupal\Core\Plugin\Factory\ContainerFactory;
14 use Drupal\Core\Routing\RouteMatchInterface;
15 use Drupal\Core\Routing\RouteProviderInterface;
17 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19 use Drupal\Core\Session\AccountInterface;
22 * Provides the default local action manager using YML as primary definition.
24 class LocalActionManager extends DefaultPluginManager implements LocalActionManagerInterface {
27 * Provides some default values for all local action plugins.
31 protected $defaults = [
32 // The plugin id. Set by the plugin system based on the top-level YAML key.
34 // The static title for the local action.
36 // The weight of the local action.
38 // (Required) the route name used to generate a link.
40 // Default route parameters for generating links.
41 'route_parameters' => [],
42 // Associative array of link options.
44 // The route names where this local action appears.
46 // Default class for local action implementations.
47 'class' => 'Drupal\Core\Menu\LocalActionDefault',
51 * A controller resolver object.
53 * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface
55 protected $controllerResolver;
60 * @var \Symfony\Component\HttpFoundation\RequestStack
62 protected $requestStack;
65 * The current route match.
67 * @var \Drupal\Core\Routing\RouteMatchInterface
69 protected $routeMatch;
72 * The route provider to load routes by name.
74 * @var \Drupal\Core\Routing\RouteProviderInterface
76 protected $routeProvider;
81 * @var \Drupal\Core\Access\AccessManagerInterface
83 protected $accessManager;
88 * @var \Drupal\Core\Session\AccountInterface
93 * The plugin instances.
95 * @var \Drupal\Core\Menu\LocalActionInterface[]
97 protected $instances = [];
100 * Constructs a LocalActionManager object.
102 * @param \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface $controller_resolver
103 * An object to use in introspecting route methods.
104 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
106 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
107 * The current route match.
108 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
109 * The route provider.
110 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
111 * The module handler.
112 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
113 * Cache backend instance to use.
114 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
115 * The language manager.
116 * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
117 * The access manager.
118 * @param \Drupal\Core\Session\AccountInterface $account
121 public function __construct(ControllerResolverInterface $controller_resolver, RequestStack $request_stack, RouteMatchInterface $route_match, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, AccountInterface $account) {
122 // Skip calling the parent constructor, since that assumes annotation-based
124 $this->factory = new ContainerFactory($this, 'Drupal\Core\Menu\LocalActionInterface');
125 $this->controllerResolver = $controller_resolver;
126 $this->requestStack = $request_stack;
127 $this->routeMatch = $route_match;
128 $this->routeProvider = $route_provider;
129 $this->accessManager = $access_manager;
130 $this->moduleHandler = $module_handler;
131 $this->account = $account;
132 $this->alterInfo('menu_local_actions');
133 $this->setCacheBackend($cache_backend, 'local_action_plugins:' . $language_manager->getCurrentLanguage()->getId(), ['local_action']);
139 protected function getDiscovery() {
140 if (!isset($this->discovery)) {
141 $yaml_discovery = new YamlDiscovery('links.action', $this->moduleHandler->getModuleDirectories());
142 $yaml_discovery->addTranslatableProperty('title', 'title_context');
143 $this->discovery = new ContainerDerivativeDiscoveryDecorator($yaml_discovery);
145 return $this->discovery;
151 public function getTitle(LocalActionInterface $local_action) {
152 $controller = [$local_action, 'getTitle'];
153 $arguments = $this->controllerResolver->getArguments($this->requestStack->getCurrentRequest(), $controller);
154 return call_user_func_array($controller, $arguments);
160 public function getActionsForRoute($route_appears) {
161 if (!isset($this->instances[$route_appears])) {
163 $this->instances[$route_appears] = [];
164 // @todo - optimize this lookup by compiling or caching.
165 foreach ($this->getDefinitions() as $plugin_id => $action_info) {
166 if (in_array($route_appears, $action_info['appears_on'])) {
167 $plugin = $this->createInstance($plugin_id);
168 $route_names[] = $plugin->getRouteName();
169 $this->instances[$route_appears][$plugin_id] = $plugin;
172 // Pre-fetch all the action route objects. This reduces the number of SQL
173 // queries that would otherwise be triggered by the access manager.
174 if (!empty($route_names)) {
175 $this->routeProvider->getRoutesByNames($route_names);
179 /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */
180 foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
181 $cacheability = new CacheableMetadata();
182 $route_name = $plugin->getRouteName();
183 $route_parameters = $plugin->getRouteParameters($this->routeMatch);
184 $access = $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
185 $links[$plugin_id] = [
186 '#theme' => 'menu_local_action',
188 'title' => $this->getTitle($plugin),
189 'url' => Url::fromRoute($route_name, $route_parameters),
190 'localized_options' => $plugin->getOptions($this->routeMatch),
192 '#access' => $access,
193 '#weight' => $plugin->getWeight(),
195 $cacheability->addCacheableDependency($access)->addCacheableDependency($plugin);
196 $cacheability->applyTo($links[$plugin_id]);
198 $links['#cache']['contexts'][] = 'route';