LocalTaskDefault.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $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. // processed the Request attributes, and in that case the _raw_variables
  41. // attribute holds the original path strings keyed to the corresponding
  42. // slugs in the path patterns. For example, if the route's path pattern is
  43. // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
  44. // $raw_variables->get('filter_format') == 'plain_text'.
  45. $raw_variables = $route_match->getRawParameters();
  46. foreach ($variables as $name) {
  47. if (isset($parameters[$name])) {
  48. continue;
  49. }
  50. if ($raw_variables && $raw_variables->has($name)) {
  51. $parameters[$name] = $raw_variables->get($name);
  52. }
  53. elseif ($value = $route_match->getRawParameter($name)) {
  54. $parameters[$name] = $value;
  55. }
  56. }
  57. // The UrlGenerator will throw an exception if expected parameters are
  58. // missing. This method should be overridden if that is possible.
  59. return $parameters;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getTitle(Request $request = NULL) {
  65. // The title from YAML file discovery may be a TranslatableMarkup object.
  66. return (string) $this->pluginDefinition['title'];
  67. }
  68. /**
  69. * Returns the weight of the local task.
  70. *
  71. * @return int
  72. * The weight of the task. If not defined in the annotation returns 0 by
  73. * default or -10 for the root tab.
  74. */
  75. public function getWeight() {
  76. // By default the weight is 0, or -10 for the root tab.
  77. if (!isset($this->pluginDefinition['weight'])) {
  78. if ($this->pluginDefinition['base_route'] == $this->pluginDefinition['route_name']) {
  79. $this->pluginDefinition['weight'] = -10;
  80. }
  81. else {
  82. $this->pluginDefinition['weight'] = 0;
  83. }
  84. }
  85. return (int) $this->pluginDefinition['weight'];
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getOptions(RouteMatchInterface $route_match) {
  91. $options = $this->pluginDefinition['options'];
  92. if ($this->active) {
  93. if (empty($options['attributes']['class']) || !in_array('is-active', $options['attributes']['class'])) {
  94. $options['attributes']['class'][] = 'is-active';
  95. }
  96. }
  97. return (array) $options;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function setActive($active = TRUE) {
  103. $this->active = $active;
  104. return $this;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getActive() {
  110. return $this->active;
  111. }
  112. /**
  113. * Returns the route provider.
  114. *
  115. * @return \Drupal\Core\Routing\RouteProviderInterface
  116. * The route provider.
  117. */
  118. protected function routeProvider() {
  119. if (!$this->routeProvider) {
  120. $this->routeProvider = \Drupal::service('router.route_provider');
  121. }
  122. return $this->routeProvider;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getCacheTags() {
  128. if (!isset($this->pluginDefinition['cache_tags'])) {
  129. return [];
  130. }
  131. return $this->pluginDefinition['cache_tags'];
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getCacheContexts() {
  137. if (!isset($this->pluginDefinition['cache_contexts'])) {
  138. return [];
  139. }
  140. return $this->pluginDefinition['cache_contexts'];
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getCacheMaxAge() {
  146. if (!isset($this->pluginDefinition['cache_max_age'])) {
  147. return Cache::PERMANENT;
  148. }
  149. return $this->pluginDefinition['cache_max_age'];
  150. }
  151. }