View source
<?php
namespace Drupal\Core\Menu;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Access\AccessManagerInterface;
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\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ContextualLinkManager extends DefaultPluginManager implements ContextualLinkManagerInterface {
protected $defaults = [
'route_name' => '',
'group' => '',
'title' => '',
'options' => [],
'weight' => NULL,
'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault',
'id' => '',
];
protected $controllerResolver;
protected $accessManager;
protected $account;
protected $requestStack;
protected $pluginsByGroup;
public function __construct(ControllerResolverInterface $controller_resolver, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManagerInterface $language_manager, AccessManagerInterface $access_manager, AccountInterface $account, RequestStack $request_stack) {
$this->factory = new ContainerFactory($this, '\\Drupal\\Core\\Menu\\ContextualLinkInterface');
$this->controllerResolver = $controller_resolver;
$this->accessManager = $access_manager;
$this->account = $account;
$this->moduleHandler = $module_handler;
$this->requestStack = $request_stack;
$this
->alterInfo('contextual_links_plugins');
$this
->setCacheBackend($cache_backend, 'contextual_links_plugins:' . $language_manager
->getCurrentLanguage()
->getId(), [
'contextual_links_plugins',
]);
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$yaml_discovery = new YamlDiscovery('links.contextual', $this->moduleHandler
->getModuleDirectories());
$yaml_discovery
->addTranslatableProperty('title', 'title_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($yaml_discovery);
}
return $this->discovery;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if (empty($definition['route_name'])) {
throw new PluginException(sprintf('Contextual link plugin (%s) definition must include "route_name".', $plugin_id));
}
if (empty($definition['group'])) {
throw new PluginException(sprintf('Contextual link plugin (%s) definition must include "group".', $plugin_id));
}
}
public function getContextualLinkPluginsByGroup($group_name) {
if (isset($this->pluginsByGroup[$group_name])) {
$contextual_links = $this->pluginsByGroup[$group_name];
}
elseif ($cache = $this->cacheBackend
->get($this->cacheKey . ':' . $group_name)) {
$contextual_links = $cache->data;
$this->pluginsByGroup[$group_name] = $contextual_links;
}
else {
$contextual_links = [];
foreach ($this
->getDefinitions() as $plugin_id => $plugin_definition) {
if ($plugin_definition['group'] == $group_name) {
$contextual_links[$plugin_id] = $plugin_definition;
}
}
$this->cacheBackend
->set($this->cacheKey . ':' . $group_name, $contextual_links);
$this->pluginsByGroup[$group_name] = $contextual_links;
}
return $contextual_links;
}
public function getContextualLinksArrayByGroup($group_name, array $route_parameters, array $metadata = []) {
$links = [];
$request = $this->requestStack
->getCurrentRequest();
foreach ($this
->getContextualLinkPluginsByGroup($group_name) as $plugin_id => $plugin_definition) {
$plugin = $this
->createInstance($plugin_id);
$route_name = $plugin
->getRouteName();
if (!$this->accessManager
->checkNamedRoute($route_name, $route_parameters, $this->account)) {
continue;
}
$links[$plugin_id] = [
'route_name' => $route_name,
'route_parameters' => $route_parameters,
'title' => $plugin
->getTitle($request),
'weight' => $plugin
->getWeight(),
'localized_options' => $plugin
->getOptions(),
'metadata' => $metadata,
];
}
$this->moduleHandler
->alter('contextual_links', $links, $group_name, $route_parameters);
return $links;
}
}