annotate core/lib/Drupal/Core/Menu/LocalActionManager.php @ 13:5fb285c0d0e3

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