You are here

class EntityRouteProviderSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 19
Contains \Drupal\Core\EventSubscriber\EntityRouteProviderSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class EntityRouteProviderSubscriber implements EventSubscriberInterface {

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs a new EntityRouteProviderSubscriber instance.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_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->entityManager
      ->getDefinitions() as $entity_type) {
      if ($entity_type
        ->hasRouteProviders()) {
        foreach ($this->entityManager
          ->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

Namesort descending Modifiers Type Description Overrides
EntityRouteProviderSubscriber::$entityManager protected property The entity manager.
EntityRouteProviderSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
EntityRouteProviderSubscriber::onDynamicRouteEvent public function Provides routes on route rebuild time.
EntityRouteProviderSubscriber::__construct public function Constructs a new EntityRouteProviderSubscriber instance.