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\Routing\RouteMatchInterface;
|
Chris@0
|
10 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Default object used for LocalTaskPlugins.
|
Chris@0
|
14 */
|
Chris@0
|
15 class LocalTaskDefault extends PluginBase implements LocalTaskInterface, CacheableDependencyInterface {
|
Chris@0
|
16
|
Chris@0
|
17 use DependencySerializationTrait;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The route provider to load routes by name.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\Core\Routing\RouteProviderInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $routeProvider;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * TRUE if this plugin is forced active for options attributes.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var bool
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $active = FALSE;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 public function getRouteName() {
|
Chris@0
|
37 return $this->pluginDefinition['route_name'];
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public function getRouteParameters(RouteMatchInterface $route_match) {
|
Chris@17
|
44 $route_parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
|
Chris@0
|
45 $route = $this->routeProvider()->getRouteByName($this->getRouteName());
|
Chris@0
|
46 $variables = $route->compile()->getVariables();
|
Chris@0
|
47
|
Chris@0
|
48 // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
|
Chris@17
|
49 // run, and the route parameters have been upcast. The original values can
|
Chris@17
|
50 // be retrieved from the raw parameters. For example, if the route's path is
|
Chris@0
|
51 // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
|
Chris@17
|
52 // $raw_parameters->get('filter_format') == 'plain_text'. Parameters that
|
Chris@17
|
53 // are not represented in the route path as slugs might be added by a route
|
Chris@17
|
54 // enhancer and will not be present in the raw parameters.
|
Chris@17
|
55 $raw_parameters = $route_match->getRawParameters();
|
Chris@17
|
56 $parameters = $route_match->getParameters();
|
Chris@0
|
57
|
Chris@0
|
58 foreach ($variables as $name) {
|
Chris@17
|
59 if (isset($route_parameters[$name])) {
|
Chris@0
|
60 continue;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@17
|
63 if ($raw_parameters->has($name)) {
|
Chris@17
|
64 $route_parameters[$name] = $raw_parameters->get($name);
|
Chris@0
|
65 }
|
Chris@17
|
66 elseif ($parameters->has($name)) {
|
Chris@17
|
67 $route_parameters[$name] = $parameters->get($name);
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@17
|
70
|
Chris@0
|
71 // The UrlGenerator will throw an exception if expected parameters are
|
Chris@0
|
72 // missing. This method should be overridden if that is possible.
|
Chris@17
|
73 return $route_parameters;
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 */
|
Chris@0
|
79 public function getTitle(Request $request = NULL) {
|
Chris@0
|
80 // The title from YAML file discovery may be a TranslatableMarkup object.
|
Chris@0
|
81 return (string) $this->pluginDefinition['title'];
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Returns the weight of the local task.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return int
|
Chris@0
|
88 * The weight of the task. If not defined in the annotation returns 0 by
|
Chris@0
|
89 * default or -10 for the root tab.
|
Chris@0
|
90 */
|
Chris@0
|
91 public function getWeight() {
|
Chris@0
|
92 // By default the weight is 0, or -10 for the root tab.
|
Chris@0
|
93 if (!isset($this->pluginDefinition['weight'])) {
|
Chris@0
|
94 if ($this->pluginDefinition['base_route'] == $this->pluginDefinition['route_name']) {
|
Chris@0
|
95 $this->pluginDefinition['weight'] = -10;
|
Chris@0
|
96 }
|
Chris@0
|
97 else {
|
Chris@0
|
98 $this->pluginDefinition['weight'] = 0;
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101 return (int) $this->pluginDefinition['weight'];
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * {@inheritdoc}
|
Chris@0
|
106 */
|
Chris@0
|
107 public function getOptions(RouteMatchInterface $route_match) {
|
Chris@0
|
108 $options = $this->pluginDefinition['options'];
|
Chris@0
|
109 if ($this->active) {
|
Chris@0
|
110 if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
|
Chris@0
|
111 $options['attributes']['class'][] = 'is-active';
|
Chris@0
|
112 }
|
Chris@0
|
113 }
|
Chris@0
|
114 return (array) $options;
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * {@inheritdoc}
|
Chris@0
|
119 */
|
Chris@0
|
120 public function setActive($active = TRUE) {
|
Chris@0
|
121 $this->active = $active;
|
Chris@0
|
122 return $this;
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * {@inheritdoc}
|
Chris@0
|
127 */
|
Chris@0
|
128 public function getActive() {
|
Chris@0
|
129 return $this->active;
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Returns the route provider.
|
Chris@0
|
134 *
|
Chris@0
|
135 * @return \Drupal\Core\Routing\RouteProviderInterface
|
Chris@0
|
136 * The route provider.
|
Chris@0
|
137 */
|
Chris@0
|
138 protected function routeProvider() {
|
Chris@0
|
139 if (!$this->routeProvider) {
|
Chris@0
|
140 $this->routeProvider = \Drupal::service('router.route_provider');
|
Chris@0
|
141 }
|
Chris@0
|
142 return $this->routeProvider;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * {@inheritdoc}
|
Chris@0
|
147 */
|
Chris@0
|
148 public function getCacheTags() {
|
Chris@0
|
149 if (!isset($this->pluginDefinition['cache_tags'])) {
|
Chris@0
|
150 return [];
|
Chris@0
|
151 }
|
Chris@0
|
152 return $this->pluginDefinition['cache_tags'];
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 /**
|
Chris@0
|
156 * {@inheritdoc}
|
Chris@0
|
157 */
|
Chris@0
|
158 public function getCacheContexts() {
|
Chris@0
|
159 if (!isset($this->pluginDefinition['cache_contexts'])) {
|
Chris@0
|
160 return [];
|
Chris@0
|
161 }
|
Chris@0
|
162 return $this->pluginDefinition['cache_contexts'];
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * {@inheritdoc}
|
Chris@0
|
167 */
|
Chris@0
|
168 public function getCacheMaxAge() {
|
Chris@0
|
169 if (!isset($this->pluginDefinition['cache_max_age'])) {
|
Chris@0
|
170 return Cache::PERMANENT;
|
Chris@0
|
171 }
|
Chris@0
|
172 return $this->pluginDefinition['cache_max_age'];
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 }
|