class EntityRouteProviderSubscriber

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber
  2. 10 core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber
  3. 11.x core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber

Ensures that routes can be provided by entity types.

Hierarchy

Expanded class hierarchy of EntityRouteProviderSubscriber

1 string reference to 'EntityRouteProviderSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses EntityRouteProviderSubscriber
entity_route_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber

File

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

Namespace

Drupal\Core\EventSubscriber
View source
class EntityRouteProviderSubscriber implements EventSubscriberInterface {
    use DeprecatedServicePropertyTrait;
    
    /**
     * {@inheritdoc}
     */
    protected $deprecatedProperties = [
        'entityManager' => 'entity.manager',
    ];
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a new EntityRouteProviderSubscriber instance.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        if ($entity_type_manager instanceof EntityManagerInterface) {
            @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);
            $this->entityTypeManager = \Drupal::entityTypeManager();
        }
        else {
            $this->entityTypeManager = $entity_type_manager;
        }
    }
    
    /**
     * Provides routes on route rebuild time.
     *
     * @param \Drupal\Core\Routing\RouteBuildEvent $event
     *   The route build event.
     */
    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);
                        }
                    }
                }
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        $events[RoutingEvents::DYNAMIC][] = [
            'onDynamicRouteEvent',
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
EntityRouteProviderSubscriber::$deprecatedProperties protected property
EntityRouteProviderSubscriber::$entityTypeManager protected property The entity type manager service.
EntityRouteProviderSubscriber::getSubscribedEvents public static function
EntityRouteProviderSubscriber::onDynamicRouteEvent public function Provides routes on route rebuild time.
EntityRouteProviderSubscriber::__construct public function Constructs a new EntityRouteProviderSubscriber instance.

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