Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Menu;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\PluginBase;
|
Chris@0
|
6 use Drupal\Core\Cache\Cache;
|
Chris@0
|
7 use Drupal\Core\Cache\CacheableDependencyInterface;
|
Chris@0
|
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
Chris@0
|
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
10 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
11 use Drupal\Core\Routing\RouteProviderInterface;
|
Chris@0
|
12 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
13 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Provides a default implementation for local action plugins.
|
Chris@0
|
17 */
|
Chris@0
|
18 class LocalActionDefault extends PluginBase implements LocalActionInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface {
|
Chris@0
|
19
|
Chris@0
|
20 use DependencySerializationTrait;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The route provider to load routes by name.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\Core\Routing\RouteProviderInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $routeProvider;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * Constructs a LocalActionDefault object.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @param array $configuration
|
Chris@0
|
33 * A configuration array containing information about the plugin instance.
|
Chris@0
|
34 * @param string $plugin_id
|
Chris@0
|
35 * The plugin_id for the plugin instance.
|
Chris@0
|
36 * @param mixed $plugin_definition
|
Chris@0
|
37 * The plugin implementation definition.
|
Chris@0
|
38 * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
|
Chris@0
|
39 * The route provider to load routes by name.
|
Chris@0
|
40 */
|
Chris@0
|
41 public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider) {
|
Chris@0
|
42 parent::__construct($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
43
|
Chris@0
|
44 $this->routeProvider = $route_provider;
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 */
|
Chris@0
|
50 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
51 return new static(
|
Chris@0
|
52 $configuration,
|
Chris@0
|
53 $plugin_id,
|
Chris@0
|
54 $plugin_definition,
|
Chris@0
|
55 $container->get('router.route_provider')
|
Chris@0
|
56 );
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * {@inheritdoc}
|
Chris@0
|
61 */
|
Chris@0
|
62 public function getRouteName() {
|
Chris@0
|
63 return $this->pluginDefinition['route_name'];
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public function getTitle(Request $request = NULL) {
|
Chris@0
|
70 // Subclasses may pull in the request or specific attributes as parameters.
|
Chris@0
|
71 // The title from YAML file discovery may be a TranslatableMarkup object.
|
Chris@0
|
72 return (string) $this->pluginDefinition['title'];
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * {@inheritdoc}
|
Chris@0
|
77 */
|
Chris@0
|
78 public function getWeight() {
|
Chris@0
|
79 return $this->pluginDefinition['weight'];
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * {@inheritdoc}
|
Chris@0
|
84 */
|
Chris@0
|
85 public function getRouteParameters(RouteMatchInterface $route_match) {
|
Chris@0
|
86 $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
|
Chris@0
|
87 $route = $this->routeProvider->getRouteByName($this->getRouteName());
|
Chris@0
|
88 $variables = $route->compile()->getVariables();
|
Chris@0
|
89
|
Chris@0
|
90 // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
|
Chris@0
|
91 // processed the Request attributes, and in that case the _raw_variables
|
Chris@0
|
92 // attribute holds the original path strings keyed to the corresponding
|
Chris@0
|
93 // slugs in the path patterns. For example, if the route's path pattern is
|
Chris@0
|
94 // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
|
Chris@0
|
95 // $raw_variables->get('filter_format') == 'plain_text'.
|
Chris@0
|
96 $raw_variables = $route_match->getRawParameters();
|
Chris@0
|
97
|
Chris@0
|
98 foreach ($variables as $name) {
|
Chris@0
|
99 if (isset($parameters[$name])) {
|
Chris@0
|
100 continue;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 if ($raw_variables && $raw_variables->has($name)) {
|
Chris@0
|
104 $parameters[$name] = $raw_variables->get($name);
|
Chris@0
|
105 }
|
Chris@0
|
106 elseif ($value = $route_match->getRawParameter($name)) {
|
Chris@0
|
107 $parameters[$name] = $value;
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|
Chris@0
|
110 // The UrlGenerator will throw an exception if expected parameters are
|
Chris@0
|
111 // missing. This method should be overridden if that is possible.
|
Chris@0
|
112 return $parameters;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 public function getOptions(RouteMatchInterface $route_match) {
|
Chris@0
|
119 return (array) $this->pluginDefinition['options'];
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * {@inheritdoc}
|
Chris@0
|
124 */
|
Chris@0
|
125 public function getCacheTags() {
|
Chris@0
|
126 if (!isset($this->pluginDefinition['cache_tags'])) {
|
Chris@0
|
127 return [];
|
Chris@0
|
128 }
|
Chris@0
|
129 return $this->pluginDefinition['cache_tags'];
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * {@inheritdoc}
|
Chris@0
|
134 */
|
Chris@0
|
135 public function getCacheContexts() {
|
Chris@0
|
136 if (!isset($this->pluginDefinition['cache_contexts'])) {
|
Chris@0
|
137 return [];
|
Chris@0
|
138 }
|
Chris@0
|
139 return $this->pluginDefinition['cache_contexts'];
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * {@inheritdoc}
|
Chris@0
|
144 */
|
Chris@0
|
145 public function getCacheMaxAge() {
|
Chris@0
|
146 if (!isset($this->pluginDefinition['cache_max_age'])) {
|
Chris@0
|
147 return Cache::PERMANENT;
|
Chris@0
|
148 }
|
Chris@0
|
149 return $this->pluginDefinition['cache_max_age'];
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 }
|