EntityRevisionRouteEnhancer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Drupal\Core\Routing\Enhancer;
  3. use Drupal\Core\Routing\EnhancerInterface;
  4. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Route;
  7. /**
  8. * Adds _entity_revision to the request attributes, if possible.
  9. */
  10. class EntityRevisionRouteEnhancer implements EnhancerInterface {
  11. /**
  12. * Returns whether the enhancer runs on the current route.
  13. *
  14. * @param \Symfony\Component\Routing\Route $route
  15. * The current route.
  16. *
  17. * @return bool
  18. */
  19. protected function applies(Route $route) {
  20. // Check whether there is any entity revision parameter.
  21. $parameters = $route->getOption('parameters') ?: [];
  22. foreach ($parameters as $info) {
  23. if (isset($info['type']) && strpos($info['type'], 'entity_revision:') === 0) {
  24. return TRUE;
  25. }
  26. }
  27. return FALSE;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function enhance(array $defaults, Request $request) {
  33. /** @var \Symfony\Component\Routing\Route $route */
  34. $route = $defaults[RouteObjectInterface::ROUTE_OBJECT];
  35. if (!$this->applies($route)) {
  36. return $defaults;
  37. }
  38. $options = $route->getOptions();
  39. if (isset($options['parameters'])) {
  40. foreach ($options['parameters'] as $name => $details) {
  41. if (!empty($details['type']) && strpos($details['type'], 'entity_revision:') !== FALSE) {
  42. $defaults['_entity_revision'] = $defaults[$name];
  43. break;
  44. }
  45. }
  46. }
  47. return $defaults;
  48. }
  49. }