Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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     $route_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     // run, and the route parameters have been upcast. The original values can
50     // be retrieved from the raw parameters. For example, if the route's path is
51     // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
52     // $raw_parameters->get('filter_format') == 'plain_text'. Parameters that
53     // are not represented in the route path as slugs might be added by a route
54     // enhancer and will not be present in the raw parameters.
55     $raw_parameters = $route_match->getRawParameters();
56     $parameters = $route_match->getParameters();
57
58     foreach ($variables as $name) {
59       if (isset($route_parameters[$name])) {
60         continue;
61       }
62
63       if ($raw_parameters->has($name)) {
64         $route_parameters[$name] = $raw_parameters->get($name);
65       }
66       elseif ($parameters->has($name)) {
67         $route_parameters[$name] = $parameters->get($name);
68       }
69     }
70
71     // The UrlGenerator will throw an exception if expected parameters are
72     // missing. This method should be overridden if that is possible.
73     return $route_parameters;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getTitle(Request $request = NULL) {
80     // The title from YAML file discovery may be a TranslatableMarkup object.
81     return (string) $this->pluginDefinition['title'];
82   }
83
84   /**
85    * Returns the weight of the local task.
86    *
87    * @return int
88    *   The weight of the task. If not defined in the annotation returns 0 by
89    *   default or -10 for the root tab.
90    */
91   public function getWeight() {
92     // By default the weight is 0, or -10 for the root tab.
93     if (!isset($this->pluginDefinition['weight'])) {
94       if ($this->pluginDefinition['base_route'] == $this->pluginDefinition['route_name']) {
95         $this->pluginDefinition['weight'] = -10;
96       }
97       else {
98         $this->pluginDefinition['weight'] = 0;
99       }
100     }
101     return (int) $this->pluginDefinition['weight'];
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function getOptions(RouteMatchInterface $route_match) {
108     $options = $this->pluginDefinition['options'];
109     if ($this->active) {
110       if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
111         $options['attributes']['class'][] = 'is-active';
112       }
113     }
114     return (array) $options;
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function setActive($active = TRUE) {
121     $this->active = $active;
122     return $this;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function getActive() {
129     return $this->active;
130   }
131
132   /**
133    * Returns the route provider.
134    *
135    * @return \Drupal\Core\Routing\RouteProviderInterface
136    *   The route provider.
137    */
138   protected function routeProvider() {
139     if (!$this->routeProvider) {
140       $this->routeProvider = \Drupal::service('router.route_provider');
141     }
142     return $this->routeProvider;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getCacheTags() {
149     if (!isset($this->pluginDefinition['cache_tags'])) {
150       return [];
151     }
152     return $this->pluginDefinition['cache_tags'];
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function getCacheContexts() {
159     if (!isset($this->pluginDefinition['cache_contexts'])) {
160       return [];
161     }
162     return $this->pluginDefinition['cache_contexts'];
163   }
164
165   /**
166    * {@inheritdoc}
167    */
168   public function getCacheMaxAge() {
169     if (!isset($this->pluginDefinition['cache_max_age'])) {
170       return Cache::PERMANENT;
171     }
172     return $this->pluginDefinition['cache_max_age'];
173   }
174
175 }