annotate core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@18 5 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
Chris@0 6 use Drupal\Core\Entity\EntityManagerInterface;
Chris@18 7 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 8 use Drupal\Core\Routing\RouteBuildEvent;
Chris@0 9 use Drupal\Core\Routing\RoutingEvents;
Chris@0 10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 11 use Symfony\Component\Routing\RouteCollection;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Ensures that routes can be provided by entity types.
Chris@0 15 */
Chris@0 16 class EntityRouteProviderSubscriber implements EventSubscriberInterface {
Chris@18 17 use DeprecatedServicePropertyTrait;
Chris@0 18
Chris@0 19 /**
Chris@18 20 * {@inheritdoc}
Chris@18 21 */
Chris@18 22 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
Chris@18 23
Chris@18 24 /**
Chris@18 25 * The entity type manager service.
Chris@0 26 *
Chris@18 27 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 28 */
Chris@18 29 protected $entityTypeManager;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Constructs a new EntityRouteProviderSubscriber instance.
Chris@0 33 *
Chris@18 34 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@18 35 * The entity type manager service.
Chris@0 36 */
Chris@18 37 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
Chris@18 38 if ($entity_type_manager instanceof EntityManagerInterface) {
Chris@18 39 @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);
Chris@18 40 $this->entityTypeManager = \Drupal::entityTypeManager();
Chris@18 41 }
Chris@18 42 else {
Chris@18 43 $this->entityTypeManager = $entity_type_manager;
Chris@18 44 }
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Provides routes on route rebuild time.
Chris@0 49 *
Chris@0 50 * @param \Drupal\Core\Routing\RouteBuildEvent $event
Chris@0 51 * The route build event.
Chris@0 52 */
Chris@0 53 public function onDynamicRouteEvent(RouteBuildEvent $event) {
Chris@0 54 $route_collection = $event->getRouteCollection();
Chris@18 55 foreach ($this->entityTypeManager->getDefinitions() as $entity_type) {
Chris@0 56 if ($entity_type->hasRouteProviders()) {
Chris@18 57 foreach ($this->entityTypeManager->getRouteProviders($entity_type->id()) as $route_provider) {
Chris@0 58 // Allow to both return an array of routes or a route collection,
Chris@0 59 // like route_callbacks in the routing.yml file.
Chris@0 60
Chris@0 61 $routes = $route_provider->getRoutes($entity_type);
Chris@0 62 if ($routes instanceof RouteCollection) {
Chris@0 63 $routes = $routes->all();
Chris@0 64 }
Chris@0 65 foreach ($routes as $route_name => $route) {
Chris@0 66 // Don't override existing routes.
Chris@0 67 if (!$route_collection->get($route_name)) {
Chris@0 68 $route_collection->add($route_name, $route);
Chris@0 69 }
Chris@0 70 }
Chris@0 71 }
Chris@0 72 }
Chris@0 73 }
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * {@inheritdoc}
Chris@0 78 */
Chris@0 79 public static function getSubscribedEvents() {
Chris@0 80 $events[RoutingEvents::DYNAMIC][] = ['onDynamicRouteEvent'];
Chris@0 81 return $events;
Chris@0 82 }
Chris@0 83
Chris@0 84 }