comparison core/lib/Drupal/Core/Menu/LocalActionManager.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
3 namespace Drupal\Core\Menu; 3 namespace Drupal\Core\Menu;
4 4
5 use Drupal\Core\Access\AccessManagerInterface; 5 use Drupal\Core\Access\AccessManagerInterface;
6 use Drupal\Core\Cache\CacheableMetadata; 6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Core\Cache\CacheBackendInterface; 7 use Drupal\Core\Cache\CacheBackendInterface;
8 use Drupal\Core\Controller\ControllerResolverInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface; 9 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Language\LanguageManagerInterface; 10 use Drupal\Core\Language\LanguageManagerInterface;
10 use Drupal\Core\Plugin\DefaultPluginManager; 11 use Drupal\Core\Plugin\DefaultPluginManager;
11 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator; 12 use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
12 use Drupal\Core\Plugin\Discovery\YamlDiscovery; 13 use Drupal\Core\Plugin\Discovery\YamlDiscovery;
13 use Drupal\Core\Plugin\Factory\ContainerFactory; 14 use Drupal\Core\Plugin\Factory\ContainerFactory;
14 use Drupal\Core\Routing\RouteMatchInterface; 15 use Drupal\Core\Routing\RouteMatchInterface;
15 use Drupal\Core\Routing\RouteProviderInterface; 16 use Drupal\Core\Routing\RouteProviderInterface;
16 use Drupal\Core\Url; 17 use Drupal\Core\Url;
17 use Symfony\Component\HttpFoundation\RequestStack; 18 use Symfony\Component\HttpFoundation\RequestStack;
18 use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; 19 use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
19 use Drupal\Core\Session\AccountInterface; 20 use Drupal\Core\Session\AccountInterface;
20 21
21 /** 22 /**
22 * Provides the default local action manager using YML as primary definition. 23 * Provides the default local action manager using YML as primary definition.
23 */ 24 */
46 // Default class for local action implementations. 47 // Default class for local action implementations.
47 'class' => 'Drupal\Core\Menu\LocalActionDefault', 48 'class' => 'Drupal\Core\Menu\LocalActionDefault',
48 ]; 49 ];
49 50
50 /** 51 /**
52 * An argument resolver object.
53 *
54 * @var \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface
55 */
56 protected $argumentResolver;
57
58 /**
51 * A controller resolver object. 59 * A controller resolver object.
52 * 60 *
53 * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface 61 * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface
62 *
63 * @deprecated
64 * Using the 'controller_resolver' service as the first argument is
65 * deprecated, use the 'http_kernel.controller.argument_resolver' instead.
66 * If your subclass requires the 'controller_resolver' service add it as an
67 * additional argument.
68 *
69 * @see https://www.drupal.org/node/2959408
54 */ 70 */
55 protected $controllerResolver; 71 protected $controllerResolver;
56 72
57 /** 73 /**
58 * The request stack. 74 * The request stack.
97 protected $instances = []; 113 protected $instances = [];
98 114
99 /** 115 /**
100 * Constructs a LocalActionManager object. 116 * Constructs a LocalActionManager object.
101 * 117 *
102 * @param \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface $controller_resolver 118 * @param \Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface $argument_resolver
103 * An object to use in introspecting route methods. 119 * An object to use in resolving route arguments.
104 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack 120 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
105 * The request stack. 121 * The request stack.
106 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match 122 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
107 * The current route match. 123 * The current route match.
108 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider 124 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
116 * @param \Drupal\Core\Access\AccessManagerInterface $access_manager 132 * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
117 * The access manager. 133 * The access manager.
118 * @param \Drupal\Core\Session\AccountInterface $account 134 * @param \Drupal\Core\Session\AccountInterface $account
119 * The current user. 135 * The current user.
120 */ 136 */
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) { 137 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) {
122 // Skip calling the parent constructor, since that assumes annotation-based 138 // Skip calling the parent constructor, since that assumes annotation-based
123 // discovery. 139 // discovery.
124 $this->factory = new ContainerFactory($this, 'Drupal\Core\Menu\LocalActionInterface'); 140 $this->factory = new ContainerFactory($this, 'Drupal\Core\Menu\LocalActionInterface');
125 $this->controllerResolver = $controller_resolver; 141 $this->argumentResolver = $argument_resolver;
142 if ($argument_resolver instanceof ControllerResolverInterface) {
143 @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);
144 $this->controllerResolver = $argument_resolver;
145 }
126 $this->requestStack = $request_stack; 146 $this->requestStack = $request_stack;
127 $this->routeMatch = $route_match; 147 $this->routeMatch = $route_match;
128 $this->routeProvider = $route_provider; 148 $this->routeProvider = $route_provider;
129 $this->accessManager = $access_manager; 149 $this->accessManager = $access_manager;
130 $this->moduleHandler = $module_handler; 150 $this->moduleHandler = $module_handler;
148 /** 168 /**
149 * {@inheritdoc} 169 * {@inheritdoc}
150 */ 170 */
151 public function getTitle(LocalActionInterface $local_action) { 171 public function getTitle(LocalActionInterface $local_action) {
152 $controller = [$local_action, 'getTitle']; 172 $controller = [$local_action, 'getTitle'];
153 $arguments = $this->controllerResolver->getArguments($this->requestStack->getCurrentRequest(), $controller); 173 $arguments = $this->argumentResolver->getArguments($this->requestStack->getCurrentRequest(), $controller);
154 return call_user_func_array($controller, $arguments); 174 return call_user_func_array($controller, $arguments);
155 } 175 }
156 176
157 /** 177 /**
158 * {@inheritdoc} 178 * {@inheritdoc}
174 if (!empty($route_names)) { 194 if (!empty($route_names)) {
175 $this->routeProvider->getRoutesByNames($route_names); 195 $this->routeProvider->getRoutesByNames($route_names);
176 } 196 }
177 } 197 }
178 $links = []; 198 $links = [];
199 $cacheability = new CacheableMetadata();
200 $cacheability->addCacheContexts(['route']);
179 /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */ 201 /** @var $plugin \Drupal\Core\Menu\LocalActionInterface */
180 foreach ($this->instances[$route_appears] as $plugin_id => $plugin) { 202 foreach ($this->instances[$route_appears] as $plugin_id => $plugin) {
181 $cacheability = new CacheableMetadata();
182 $route_name = $plugin->getRouteName(); 203 $route_name = $plugin->getRouteName();
183 $route_parameters = $plugin->getRouteParameters($this->routeMatch); 204 $route_parameters = $plugin->getRouteParameters($this->routeMatch);
184 $access = $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE); 205 $access = $this->accessManager->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
185 $links[$plugin_id] = [ 206 $links[$plugin_id] = [
186 '#theme' => 'menu_local_action', 207 '#theme' => 'menu_local_action',
191 ], 212 ],
192 '#access' => $access, 213 '#access' => $access,
193 '#weight' => $plugin->getWeight(), 214 '#weight' => $plugin->getWeight(),
194 ]; 215 ];
195 $cacheability->addCacheableDependency($access)->addCacheableDependency($plugin); 216 $cacheability->addCacheableDependency($access)->addCacheableDependency($plugin);
196 $cacheability->applyTo($links[$plugin_id]); 217 }
197 } 218 $cacheability->applyTo($links);
198 $links['#cache']['contexts'][] = 'route';
199 219
200 return $links; 220 return $links;
201 } 221 }
202 222
203 } 223 }