3 namespace Drupal\Core\Menu;
5 use Drupal\Component\Plugin\PluginBase;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Cache\CacheableDependencyInterface;
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\Core\Routing\RouteMatchInterface;
11 use Drupal\Core\Routing\RouteProviderInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\HttpFoundation\Request;
16 * Provides a default implementation for local action plugins.
18 class LocalActionDefault extends PluginBase implements LocalActionInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface {
20 use DependencySerializationTrait;
23 * The route provider to load routes by name.
25 * @var \Drupal\Core\Routing\RouteProviderInterface
27 protected $routeProvider;
30 * Constructs a LocalActionDefault object.
32 * @param array $configuration
33 * A configuration array containing information about the plugin instance.
34 * @param string $plugin_id
35 * The plugin_id for the plugin instance.
36 * @param mixed $plugin_definition
37 * The plugin implementation definition.
38 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
39 * The route provider to load routes by name.
41 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider) {
42 parent::__construct($configuration, $plugin_id, $plugin_definition);
44 $this->routeProvider = $route_provider;
50 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55 $container->get('router.route_provider')
62 public function getRouteName() {
63 return $this->pluginDefinition['route_name'];
69 public function getTitle(Request $request = NULL) {
70 // Subclasses may pull in the request or specific attributes as parameters.
71 // The title from YAML file discovery may be a TranslatableMarkup object.
72 return (string) $this->pluginDefinition['title'];
78 public function getWeight() {
79 return $this->pluginDefinition['weight'];
85 public function getRouteParameters(RouteMatchInterface $route_match) {
86 $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
87 $route = $this->routeProvider->getRouteByName($this->getRouteName());
88 $variables = $route->compile()->getVariables();
90 // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
91 // processed the Request attributes, and in that case the _raw_variables
92 // attribute holds the original path strings keyed to the corresponding
93 // slugs in the path patterns. For example, if the route's path pattern is
94 // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
95 // $raw_variables->get('filter_format') == 'plain_text'.
96 $raw_variables = $route_match->getRawParameters();
98 foreach ($variables as $name) {
99 if (isset($parameters[$name])) {
103 if ($raw_variables && $raw_variables->has($name)) {
104 $parameters[$name] = $raw_variables->get($name);
106 elseif ($value = $route_match->getRawParameter($name)) {
107 $parameters[$name] = $value;
110 // The UrlGenerator will throw an exception if expected parameters are
111 // missing. This method should be overridden if that is possible.
118 public function getOptions(RouteMatchInterface $route_match) {
119 return (array) $this->pluginDefinition['options'];
125 public function getCacheTags() {
126 if (!isset($this->pluginDefinition['cache_tags'])) {
129 return $this->pluginDefinition['cache_tags'];
135 public function getCacheContexts() {
136 if (!isset($this->pluginDefinition['cache_contexts'])) {
139 return $this->pluginDefinition['cache_contexts'];
145 public function getCacheMaxAge() {
146 if (!isset($this->pluginDefinition['cache_max_age'])) {
147 return Cache::PERMANENT;
149 return $this->pluginDefinition['cache_max_age'];