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@17
|
86 $route_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@17
|
91 // run, and the route parameters have been upcast. The original values can
|
Chris@17
|
92 // be retrieved from the raw parameters. For example, if the route's path is
|
Chris@0
|
93 // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
|
Chris@17
|
94 // $raw_parameters->get('filter_format') == 'plain_text'. Parameters that
|
Chris@17
|
95 // are not represented in the route path as slugs might be added by a route
|
Chris@17
|
96 // enhancer and will not be present in the raw parameters.
|
Chris@17
|
97 $raw_parameters = $route_match->getRawParameters();
|
Chris@17
|
98 $parameters = $route_match->getParameters();
|
Chris@0
|
99
|
Chris@0
|
100 foreach ($variables as $name) {
|
Chris@17
|
101 if (isset($route_parameters[$name])) {
|
Chris@0
|
102 continue;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@17
|
105 if ($raw_parameters->has($name)) {
|
Chris@17
|
106 $route_parameters[$name] = $raw_parameters->get($name);
|
Chris@0
|
107 }
|
Chris@17
|
108 elseif ($parameters->has($name)) {
|
Chris@17
|
109 $route_parameters[$name] = $parameters->get($name);
|
Chris@0
|
110 }
|
Chris@0
|
111 }
|
Chris@17
|
112
|
Chris@0
|
113 // The UrlGenerator will throw an exception if expected parameters are
|
Chris@0
|
114 // missing. This method should be overridden if that is possible.
|
Chris@17
|
115 return $route_parameters;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function getOptions(RouteMatchInterface $route_match) {
|
Chris@0
|
122 return (array) $this->pluginDefinition['options'];
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function getCacheTags() {
|
Chris@0
|
129 if (!isset($this->pluginDefinition['cache_tags'])) {
|
Chris@0
|
130 return [];
|
Chris@0
|
131 }
|
Chris@0
|
132 return $this->pluginDefinition['cache_tags'];
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * {@inheritdoc}
|
Chris@0
|
137 */
|
Chris@0
|
138 public function getCacheContexts() {
|
Chris@0
|
139 if (!isset($this->pluginDefinition['cache_contexts'])) {
|
Chris@0
|
140 return [];
|
Chris@0
|
141 }
|
Chris@0
|
142 return $this->pluginDefinition['cache_contexts'];
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * {@inheritdoc}
|
Chris@0
|
147 */
|
Chris@0
|
148 public function getCacheMaxAge() {
|
Chris@0
|
149 if (!isset($this->pluginDefinition['cache_max_age'])) {
|
Chris@0
|
150 return Cache::PERMANENT;
|
Chris@0
|
151 }
|
Chris@0
|
152 return $this->pluginDefinition['cache_max_age'];
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 }
|