LocalActionManager.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Drupal\Core\Menu;
  3. use Drupal\Core\Access\AccessManagerInterface;
  4. use Drupal\Core\Cache\CacheableMetadata;
  5. use Drupal\Core\Cache\CacheBackendInterface;
  6. use Drupal\Core\Controller\ControllerResolverInterface;
  7. use Drupal\Core\Extension\ModuleHandlerInterface;
  8. use Drupal\Core\Language\LanguageManagerInterface;
  9. use Drupal\Core\Plugin\DefaultPluginManager;
  10. use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
  11. use Drupal\Core\Plugin\Discovery\YamlDiscovery;
  12. use Drupal\Core\Plugin\Factory\ContainerFactory;
  13. use Drupal\Core\Routing\RouteMatchInterface;
  14. use Drupal\Core\Routing\RouteProviderInterface;
  15. use Drupal\Core\Url;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
  18. use Drupal\Core\Session\AccountInterface;
  19. /**
  20. * Provides the default local action manager using YML as primary definition.
  21. */
  22. class LocalActionManager extends DefaultPluginManager implements LocalActionManagerInterface {
  23. /**
  24. * Provides some default values for all local action plugins.
  25. *
  26. * @var array
  27. */
  28. protected $defaults = [
  29. // The plugin id. Set by the plugin system based on the top-level YAML key.
  30. 'id' => NULL,
  31. // The static title for the local action.
  32. 'title' => '',
  33. // The weight of the local action.
  34. 'weight' => NULL,
  35. // (Required) the route name used to generate a link.
  36. 'route_name' => NULL,
  37. // Default route parameters for generating links.
  38. 'route_parameters' => [],
  39. // Associative array of link options.
  40. 'options' => [],
  41. // The route names where this local action appears.
  42. 'appears_on' => [],
  43. // Default class for local action implementations.
  44. 'class' => 'Drupal\Core\Menu\LocalActionDefault',
  45. ];
  46. /**
  47. * An argument resolver object.
  48. *
  49. * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
  50. */
  51. protected $argumentResolver;
  52. /**
  53. * A controller resolver object.
  54. *
  55. * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface
  56. *
  57. * @deprecated
  58. * Using the 'controller_resolver' service as the first argument is
  59. * deprecated, use the 'http_kernel.controller.argument_resolver' instead.
  60. * If your subclass requires the 'controller_resolver' service add it as an
  61. * additional argument.
  62. *
  63. * @see https://www.drupal.org/node/2959408
  64. */
  65. protected $controllerResolver;
  66. /**
  67. * The request stack.
  68. *
  69. * @var \Symfony\Component\HttpFoundation\RequestStack
  70. */
  71. protected $requestStack;
  72. /**
  73. * The current route match.
  74. *
  75. * @var \Drupal\Core\Routing\RouteMatchInterface
  76. */
  77. protected $routeMatch;
  78. /**
  79. * The route provider to load routes by name.
  80. *
  81. * @var \Drupal\Core\Routing\RouteProviderInterface
  82. */
  83. protected $routeProvider;
  84. /**
  85. * The access manager.
  86. *
  87. * @var \Drupal\Core\Access\AccessManagerInterface
  88. */
  89. protected $accessManager;
  90. /**
  91. * The current user.
  92. *
  93. * @var \Drupal\Core\Session\AccountInterface
  94. */
  95. protected $account;
  96. /**
  97. * The plugin instances.
  98. *
  99. * @var \Drupal\Core\Menu\LocalActionInterface[]
  100. */
  101. protected $instances = [];
  102. /**
  103. * Constructs a LocalActionManager object.
  104. *
  105. * @param \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface $argument_resolver
  106. * An object to use in resolving route arguments.
  107. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
  108. * The request stack.
  109. * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
  110. * The current route match.
  111. * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
  112. * The route provider.
  113. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  114. * The module handler.
  115. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  116. * Cache backend instance to use.
  117. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
  118. * The language manager.
  119. * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
  120. * The access manager.
  121. * @param \Drupal\Core\Session\AccountInterface $account
  122. * The current user.
  123. */
  124. public function __construct(ArgumentResolverInterface $argument_resolver, RequestStack $request_stack, RouteMatchInterface $route_match, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, AccountInterface $account) {
  125. // Skip calling the parent constructor, since that assumes annotation-based
  126. // discovery.
  127. $this->factory = new ContainerFactory($this, 'Drupal\Core\Menu\LocalActionInterface');
  128. $this->argumentResolver = $argument_resolver;
  129. if ($argument_resolver instanceof ControllerResolverInterface) {
  130. @trigger_error("Using the 'controller_resolver' service as the first argument is deprecated, use the 'http_kernel.controller.argument_resolver' instead. If your subclass requires the 'controller_resolver' service add it as an additional argument. See https://www.drupal.org/node/2959408.", E_USER_DEPRECATED);
  131. $this->controllerResolver = $argument_resolver;
  132. }
  133. $this->requestStack = $request_stack;
  134. $this->routeMatch = $route_match;
  135. $this->routeProvider = $route_provider;
  136. $this->accessManager = $access_manager;
  137. $this->moduleHandler = $module_handler;
  138. $this->account = $account;
  139. $this->alterInfo('menu_local_actions');
  140. $this->setCacheBackend($cache_backend, 'local_action_plugins:' . $language_manager->getCurrentLanguage()->getId(), ['local_action']);
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. protected function getDiscovery() {
  146. if (!isset($this->discovery)) {
  147. $yaml_discovery = new YamlDiscovery('links.action', $this->moduleHandler->getModuleDirectories());
  148. $yaml_discovery->addTranslatableProperty('title', 'title_context');
  149. $this->discovery = new ContainerDerivativeDiscoveryDecorator($yaml_discovery);
  150. }
  151. return $this->discovery;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function getTitle(LocalActionInterface $local_action) {
  157. $controller = [$local_action, 'getTitle'];
  158. $arguments = $this->argumentResolver->getArguments($this->requestStack->getCurrentRequest(), $controller);
  159. return call_user_func_array($controller, $arguments);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getActionsForRoute($route_appears) {
  165. if (!isset($this->instances[$route_appears])) {
  166. $route_names = [];
  167. $this->instances[$route_appears] = [];
  168. // @todo - optimize this lookup by compiling or caching.
  169. foreach ($this->getDefinitions() as $plugin_id => $action_info) {
  170. if (in_array($route_appears, $action_info['appears_on'])) {
  171. $plugin = $this->createInstance($plugin_id);
  172. $route_names[] = $plugin->getRouteName();
  173. $this->instances[$route_appears][$plugin_id] = $plugin;
  174. }
  175. }
  176. // Pre-fetch all the action route objects. This reduces the number of SQL
  177. // queries that would otherwise be triggered by the access manager.
  178. if (!empty($route_names)) {
  179. $this->routeProvider->getRoutesByNames($route_names);
  180. }
  181. }
  182. $links = [];
  183. $cacheability = new CacheableMetadata();
  184. $cacheability->addCacheContexts(['route']);
  185. /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */
  186. foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
  187. $route_name = $plugin->getRouteName();
  188. $route_parameters = $plugin->getRouteParameters($this->routeMatch);
  189. $access = $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
  190. $links[$plugin_id] = [
  191. '#theme' => 'menu_local_action',
  192. '#link' => [
  193. 'title' => $this->getTitle($plugin),
  194. 'url' => Url::fromRoute($route_name, $route_parameters),
  195. 'localized_options' => $plugin->getOptions($this->routeMatch),
  196. ],
  197. '#access' => $access,
  198. '#weight' => $plugin->getWeight(),
  199. ];
  200. $cacheability->addCacheableDependency($access)->addCacheableDependency($plugin);
  201. }
  202. $cacheability->applyTo($links);
  203. return $links;
  204. }
  205. }