3 namespace Drupal\Core\Menu;
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;
13 * Default object used for LocalTaskPlugins.
15 class LocalTaskDefault extends PluginBase implements LocalTaskInterface, CacheableDependencyInterface {
17 use DependencySerializationTrait;
20 * The route provider to load routes by name.
22 * @var \Drupal\Core\Routing\RouteProviderInterface
24 protected $routeProvider;
27 * TRUE if this plugin is forced active for options attributes.
31 protected $active = FALSE;
36 public function getRouteName() {
37 return $this->pluginDefinition['route_name'];
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();
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'.
55 $raw_variables = $route_match->getRawParameters();
57 foreach ($variables as $name) {
58 if (isset($parameters[$name])) {
62 if ($raw_variables && $raw_variables->has($name)) {
63 $parameters[$name] = $raw_variables->get($name);
65 elseif ($value = $route_match->getRawParameter($name)) {
66 $parameters[$name] = $value;
69 // The UrlGenerator will throw an exception if expected parameters are
70 // missing. This method should be overridden if that is possible.
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'];
83 * Returns the weight of the local task.
86 * The weight of the task. If not defined in the annotation returns 0 by
87 * default or -10 for the root tab.
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;
96 $this->pluginDefinition['weight'] = 0;
99 return (int) $this->pluginDefinition['weight'];
105 public function getOptions(RouteMatchInterface $route_match) {
106 $options = $this->pluginDefinition['options'];
108 if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
109 $options['attributes']['class'][] = 'is-active';
112 return (array) $options;
118 public function setActive($active = TRUE) {
119 $this->active = $active;
126 public function getActive() {
127 return $this->active;
131 * Returns the route provider.
133 * @return \Drupal\Core\Routing\RouteProviderInterface
134 * The route provider.
136 protected function routeProvider() {
137 if (!$this->routeProvider) {
138 $this->routeProvider = \Drupal::service('router.route_provider');
140 return $this->routeProvider;
146 public function getCacheTags() {
147 if (!isset($this->pluginDefinition['cache_tags'])) {
150 return $this->pluginDefinition['cache_tags'];
156 public function getCacheContexts() {
157 if (!isset($this->pluginDefinition['cache_contexts'])) {
160 return $this->pluginDefinition['cache_contexts'];
166 public function getCacheMaxAge() {
167 if (!isset($this->pluginDefinition['cache_max_age'])) {
168 return Cache::PERMANENT;
170 return $this->pluginDefinition['cache_max_age'];