EntityRouteProviderSubscriber.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  4. use Drupal\Core\Entity\EntityManagerInterface;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Routing\RouteBuildEvent;
  7. use Drupal\Core\Routing\RoutingEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Routing\RouteCollection;
  10. /**
  11. * Ensures that routes can be provided by entity types.
  12. */
  13. class EntityRouteProviderSubscriber implements EventSubscriberInterface {
  14. use DeprecatedServicePropertyTrait;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  19. /**
  20. * The entity type manager service.
  21. *
  22. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  23. */
  24. protected $entityTypeManager;
  25. /**
  26. * Constructs a new EntityRouteProviderSubscriber instance.
  27. *
  28. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  29. * The entity type manager service.
  30. */
  31. public function __construct(EntityTypeManagerInterface $entity_type_manager) {
  32. if ($entity_type_manager instanceof EntityManagerInterface) {
  33. @trigger_error('Passing the entity.manager service to EntityRouteProviderSubscriber::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  34. $this->entityTypeManager = \Drupal::entityTypeManager();
  35. }
  36. else {
  37. $this->entityTypeManager = $entity_type_manager;
  38. }
  39. }
  40. /**
  41. * Provides routes on route rebuild time.
  42. *
  43. * @param \Drupal\Core\Routing\RouteBuildEvent $event
  44. * The route build event.
  45. */
  46. public function onDynamicRouteEvent(RouteBuildEvent $event) {
  47. $route_collection = $event->getRouteCollection();
  48. foreach ($this->entityTypeManager->getDefinitions() as $entity_type) {
  49. if ($entity_type->hasRouteProviders()) {
  50. foreach ($this->entityTypeManager->getRouteProviders($entity_type->id()) as $route_provider) {
  51. // Allow to both return an array of routes or a route collection,
  52. // like route_callbacks in the routing.yml file.
  53. $routes = $route_provider->getRoutes($entity_type);
  54. if ($routes instanceof RouteCollection) {
  55. $routes = $routes->all();
  56. }
  57. foreach ($routes as $route_name => $route) {
  58. // Don't override existing routes.
  59. if (!$route_collection->get($route_name)) {
  60. $route_collection->add($route_name, $route);
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public static function getSubscribedEvents() {
  71. $events[RoutingEvents::DYNAMIC][] = ['onDynamicRouteEvent'];
  72. return $events;
  73. }
  74. }