function EntityRouteProviderSubscriber::onDynamicRouteEvent

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber::onDynamicRouteEvent()
  2. 10 core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber::onDynamicRouteEvent()
  3. 11.x core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber::onDynamicRouteEvent()

Provides routes on route rebuild time.

Parameters

\Drupal\Core\Routing\RouteBuildEvent $event: The route build event.

File

core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php, line 39

Class

EntityRouteProviderSubscriber
Ensures that routes can be provided by entity types.

Namespace

Drupal\Core\EventSubscriber

Code

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) {
                // Allow to both return an array of routes or a route collection,
                // like route_callbacks in the routing.yml file.
                $routes = $route_provider->getRoutes($entity_type);
                if ($routes instanceof RouteCollection) {
                    $routes = $routes->all();
                }
                foreach ($routes as $route_name => $route) {
                    // Don't override existing routes.
                    if (!$route_collection->get($route_name)) {
                        $route_collection->add($route_name, $route);
                    }
                }
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.