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 $route_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 // 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();
58 foreach ($variables as $name) {
59 if (isset($route_parameters[$name])) {
63 if ($raw_parameters->has($name)) {
64 $route_parameters[$name] = $raw_parameters->get($name);
66 elseif ($parameters->has($name)) {
67 $route_parameters[$name] = $parameters->get($name);
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;
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'];
85 * Returns the weight of the local task.
88 * The weight of the task. If not defined in the annotation returns 0 by
89 * default or -10 for the root tab.
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;
98 $this->pluginDefinition['weight'] = 0;
101 return (int) $this->pluginDefinition['weight'];
107 public function getOptions(RouteMatchInterface $route_match) {
108 $options = $this->pluginDefinition['options'];
110 if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
111 $options['attributes']['class'][] = 'is-active';
114 return (array) $options;
120 public function setActive($active = TRUE) {
121 $this->active = $active;
128 public function getActive() {
129 return $this->active;
133 * Returns the route provider.
135 * @return \Drupal\Core\Routing\RouteProviderInterface
136 * The route provider.
138 protected function routeProvider() {
139 if (!$this->routeProvider) {
140 $this->routeProvider = \Drupal::service('router.route_provider');
142 return $this->routeProvider;
148 public function getCacheTags() {
149 if (!isset($this->pluginDefinition['cache_tags'])) {
152 return $this->pluginDefinition['cache_tags'];
158 public function getCacheContexts() {
159 if (!isset($this->pluginDefinition['cache_contexts'])) {
162 return $this->pluginDefinition['cache_contexts'];
168 public function getCacheMaxAge() {
169 if (!isset($this->pluginDefinition['cache_max_age'])) {
170 return Cache::PERMANENT;
172 return $this->pluginDefinition['cache_max_age'];