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\Controller\ControllerResolverInterface;
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\YamlDiscovery;
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;
class LocalActionManager extends DefaultPluginManager implements LocalActionManagerInterface {
protected $defaults = [
'id' => NULL,
'title' => '',
'weight' => NULL,
'route_name' => NULL,
'route_parameters' => [],
'options' => [],
'appears_on' => [],
'class' => 'Drupal\\Core\\Menu\\LocalActionDefault',
];
protected $argumentResolver;
protected $controllerResolver;
protected $requestStack;
protected $routeMatch;
protected $routeProvider;
protected $accessManager;
protected $account;
protected $instances = [];
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) {
$this->factory = new ContainerFactory($this, 'Drupal\\Core\\Menu\\LocalActionInterface');
$this->argumentResolver = $argument_resolver;
if ($argument_resolver instanceof ControllerResolverInterface) {
@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);
$this->controllerResolver = $argument_resolver;
}
$this->requestStack = $request_stack;
$this->routeMatch = $route_match;
$this->routeProvider = $route_provider;
$this->accessManager = $access_manager;
$this->moduleHandler = $module_handler;
$this->account = $account;
$this
->alterInfo('menu_local_actions');
$this
->setCacheBackend($cache_backend, 'local_action_plugins:' . $language_manager
->getCurrentLanguage()
->getId(), [
'local_action',
]);
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$yaml_discovery = new YamlDiscovery('links.action', $this->moduleHandler
->getModuleDirectories());
$yaml_discovery
->addTranslatableProperty('title', 'title_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($yaml_discovery);
}
return $this->discovery;
}
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);
}
public function getActionsForRoute($route_appears) {
if (!isset($this->instances[$route_appears])) {
$route_names = [];
$this->instances[$route_appears] = [];
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;
}
}
if (!empty($route_names)) {
$this->routeProvider
->getRoutesByNames($route_names);
}
}
$links = [];
$cacheability = new CacheableMetadata();
$cacheability
->addCacheContexts([
'route',
]);
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;
}
}