LocalTaskDefault.php 4.7 KB

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