Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityResolverManager;
|
Chris@0
|
6 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
7 use Drupal\Core\Routing\RoutingEvents;
|
Chris@0
|
8 use Drupal\Core\Routing\RouteBuildEvent;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Registers the 'type' of route parameter names that match an entity type.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @todo Matching on parameter *name* is not ideal, because it breaks
|
Chris@0
|
14 * encapsulation: parameter names are local to the controller and route, and
|
Chris@0
|
15 * controllers and routes can't be expected to know what all possible entity
|
Chris@0
|
16 * types might exist across all modules in order to pick names that don't
|
Chris@0
|
17 * conflict. Instead, the 'type' should be determined from introspecting what
|
Chris@0
|
18 * kind of PHP variable (e.g., a type hinted interface) the controller
|
Chris@0
|
19 * requires: https://www.drupal.org/node/2041907.
|
Chris@0
|
20 */
|
Chris@0
|
21 class EntityRouteAlterSubscriber implements EventSubscriberInterface {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The entity resolver manager.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Entity\EntityResolverManager
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $resolverManager;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Constructs an EntityRouteAlterSubscriber instance.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param \Drupal\Core\Entity\EntityResolverManager $entity_resolver_manager
|
Chris@0
|
34 * The entity resolver manager.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct(EntityResolverManager $entity_resolver_manager) {
|
Chris@0
|
37 $this->resolverManager = $entity_resolver_manager;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Applies parameter converters to route parameters.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param \Drupal\Core\Routing\RouteBuildEvent $event
|
Chris@0
|
44 * The event to process.
|
Chris@0
|
45 */
|
Chris@0
|
46 public function onRoutingRouteAlterSetType(RouteBuildEvent $event) {
|
Chris@0
|
47 foreach ($event->getRouteCollection() as $route) {
|
Chris@0
|
48 $this->resolverManager->setRouteOptions($route);
|
Chris@0
|
49 }
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * {@inheritdoc}
|
Chris@0
|
54 */
|
Chris@0
|
55 public static function getSubscribedEvents() {
|
Chris@0
|
56 $events[RoutingEvents::ALTER][] = ['onRoutingRouteAlterSetType', -150];
|
Chris@0
|
57 return $events;
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 }
|