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