LocalActionDefault.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Plugin\ContainerFactoryPluginInterface;
  8. use Drupal\Core\Routing\RouteMatchInterface;
  9. use Drupal\Core\Routing\RouteProviderInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13. * Provides a default implementation for local action plugins.
  14. */
  15. class LocalActionDefault extends PluginBase implements LocalActionInterface, ContainerFactoryPluginInterface, CacheableDependencyInterface {
  16. use DependencySerializationTrait;
  17. /**
  18. * The route provider to load routes by name.
  19. *
  20. * @var \Drupal\Core\Routing\RouteProviderInterface
  21. */
  22. protected $routeProvider;
  23. /**
  24. * Constructs a LocalActionDefault object.
  25. *
  26. * @param array $configuration
  27. * A configuration array containing information about the plugin instance.
  28. * @param string $plugin_id
  29. * The plugin_id for the plugin instance.
  30. * @param mixed $plugin_definition
  31. * The plugin implementation definition.
  32. * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
  33. * The route provider to load routes by name.
  34. */
  35. public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider) {
  36. parent::__construct($configuration, $plugin_id, $plugin_definition);
  37. $this->routeProvider = $route_provider;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  43. return new static(
  44. $configuration,
  45. $plugin_id,
  46. $plugin_definition,
  47. $container->get('router.route_provider')
  48. );
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getRouteName() {
  54. return $this->pluginDefinition['route_name'];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getTitle(Request $request = NULL) {
  60. // Subclasses may pull in the request or specific attributes as parameters.
  61. // The title from YAML file discovery may be a TranslatableMarkup object.
  62. return (string) $this->pluginDefinition['title'];
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getWeight() {
  68. return $this->pluginDefinition['weight'];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getRouteParameters(RouteMatchInterface $route_match) {
  74. $route_parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : [];
  75. $route = $this->routeProvider->getRouteByName($this->getRouteName());
  76. $variables = $route->compile()->getVariables();
  77. // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
  78. // run, and the route parameters have been upcast. The original values can
  79. // be retrieved from the raw parameters. For example, if the route's path is
  80. // /filter/tips/{filter_format} and the path is /filter/tips/plain_text then
  81. // $raw_parameters->get('filter_format') == 'plain_text'. Parameters that
  82. // are not represented in the route path as slugs might be added by a route
  83. // enhancer and will not be present in the raw parameters.
  84. $raw_parameters = $route_match->getRawParameters();
  85. $parameters = $route_match->getParameters();
  86. foreach ($variables as $name) {
  87. if (isset($route_parameters[$name])) {
  88. continue;
  89. }
  90. if ($raw_parameters->has($name)) {
  91. $route_parameters[$name] = $raw_parameters->get($name);
  92. }
  93. elseif ($parameters->has($name)) {
  94. $route_parameters[$name] = $parameters->get($name);
  95. }
  96. }
  97. // The UrlGenerator will throw an exception if expected parameters are
  98. // missing. This method should be overridden if that is possible.
  99. return $route_parameters;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getOptions(RouteMatchInterface $route_match) {
  105. return (array) $this->pluginDefinition['options'];
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getCacheTags() {
  111. if (!isset($this->pluginDefinition['cache_tags'])) {
  112. return [];
  113. }
  114. return $this->pluginDefinition['cache_tags'];
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getCacheContexts() {
  120. if (!isset($this->pluginDefinition['cache_contexts'])) {
  121. return [];
  122. }
  123. return $this->pluginDefinition['cache_contexts'];
  124. }
  125. /**
  126. * {@inheritdoc}
  127. */
  128. public function getCacheMaxAge() {
  129. if (!isset($this->pluginDefinition['cache_max_age'])) {
  130. return Cache::PERMANENT;
  131. }
  132. return $this->pluginDefinition['cache_max_age'];
  133. }
  134. }