EntityRouteProviderSubscriber.php in Drupal 9
File
core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php
View source
<?php
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Core\Routing\RoutingEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\RouteCollection;
class EntityRouteProviderSubscriber implements EventSubscriberInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public function onDynamicRouteEvent(RouteBuildEvent $event) {
$route_collection = $event
->getRouteCollection();
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type) {
if ($entity_type
->hasRouteProviders()) {
foreach ($this->entityTypeManager
->getRouteProviders($entity_type
->id()) as $route_provider) {
$routes = $route_provider
->getRoutes($entity_type);
if ($routes instanceof RouteCollection) {
$routes = $routes
->all();
}
foreach ($routes as $route_name => $route) {
if (!$route_collection
->get($route_name)) {
$route_collection
->add($route_name, $route);
}
}
}
}
}
}
public static function getSubscribedEvents() {
$events[RoutingEvents::DYNAMIC][] = [
'onDynamicRouteEvent',
];
return $events;
}
}