Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Menu / LocalActionDefault.php
1 <?php
2
3 namespace Drupal\Core\Menu;
4
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;
14
15 /**
16  * Provides a default implementation for local action plugins.
17  */
18 class LocalActionDefault extends PluginBase implements LocalActionInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface {
19
20   use DependencySerializationTrait;
21
22   /**
23    * The route provider to load routes by name.
24    *
25    * @var \Drupal\Core\Routing\RouteProviderInterface
26    */
27   protected $routeProvider;
28
29   /**
30    * Constructs a LocalActionDefault object.
31    *
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.
40    */
41   public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider) {
42     parent::__construct($configuration, $plugin_id, $plugin_definition);
43
44     $this->routeProvider = $route_provider;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51     return new static(
52       $configuration,
53       $plugin_id,
54       $plugin_definition,
55       $container->get('router.route_provider')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getRouteName() {
63     return $this->pluginDefinition['route_name'];
64   }
65
66   /**
67    * {@inheritdoc}
68    */
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'];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getWeight() {
79     return $this->pluginDefinition['weight'];
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getRouteParameters(RouteMatchInterface $route_match) {
86     $route_parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
87     $route = $this->routeProvider->getRouteByName($this->getRouteName());
88     $variables = $route->compile()->getVariables();
89
90     // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
91     // run, and the route parameters have been upcast. The original values can
92     // be retrieved from the raw parameters. For example, if the route's path is
93     // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
94     // $raw_parameters->get('filter_format') == 'plain_text'. Parameters that
95     // are not represented in the route path as slugs might be added by a route
96     // enhancer and will not be present in the raw parameters.
97     $raw_parameters = $route_match->getRawParameters();
98     $parameters = $route_match->getParameters();
99
100     foreach ($variables as $name) {
101       if (isset($route_parameters[$name])) {
102         continue;
103       }
104
105       if ($raw_parameters->has($name)) {
106         $route_parameters[$name] = $raw_parameters->get($name);
107       }
108       elseif ($parameters->has($name)) {
109         $route_parameters[$name] = $parameters->get($name);
110       }
111     }
112
113     // The UrlGenerator will throw an exception if expected parameters are
114     // missing. This method should be overridden if that is possible.
115     return $route_parameters;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getOptions(RouteMatchInterface $route_match) {
122     return (array) $this->pluginDefinition['options'];
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function getCacheTags() {
129     if (!isset($this->pluginDefinition['cache_tags'])) {
130       return [];
131     }
132     return $this->pluginDefinition['cache_tags'];
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function getCacheContexts() {
139     if (!isset($this->pluginDefinition['cache_contexts'])) {
140       return [];
141     }
142     return $this->pluginDefinition['cache_contexts'];
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getCacheMaxAge() {
149     if (!isset($this->pluginDefinition['cache_max_age'])) {
150       return Cache::PERMANENT;
151     }
152     return $this->pluginDefinition['cache_max_age'];
153   }
154
155 }