Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\rest\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
6 use Drupal\Core\Routing\RouteBuildEvent;
|
Chris@0
|
7 use Drupal\Core\Routing\RoutingEvents;
|
Chris@0
|
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Generates a 'create' route for an entity type if it has a REST POST route.
|
Chris@0
|
12 */
|
Chris@0
|
13 class EntityResourcePostRouteSubscriber implements EventSubscriberInterface {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The REST resource config storage.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $resourceConfigStorage;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Constructs a new EntityResourcePostRouteSubscriber instance.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@0
|
26 * The entity type manager.
|
Chris@0
|
27 */
|
Chris@0
|
28 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
|
Chris@0
|
29 $this->resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_config');
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Provides routes on route rebuild time.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @param \Drupal\Core\Routing\RouteBuildEvent $event
|
Chris@0
|
36 * The route build event.
|
Chris@0
|
37 */
|
Chris@0
|
38 public function onDynamicRouteEvent(RouteBuildEvent $event) {
|
Chris@0
|
39 $route_collection = $event->getRouteCollection();
|
Chris@0
|
40
|
Chris@0
|
41 $resource_configs = $this->resourceConfigStorage->loadMultiple();
|
Chris@0
|
42 // Iterate over all REST resource config entities.
|
Chris@0
|
43 foreach ($resource_configs as $resource_config) {
|
Chris@0
|
44 // We only care about REST resource config entities for the
|
Chris@0
|
45 // \Drupal\rest\Plugin\rest\resource\EntityResource plugin.
|
Chris@0
|
46 $plugin_id = $resource_config->toArray()['plugin_id'];
|
Chris@0
|
47 if (substr($plugin_id, 0, 6) !== 'entity') {
|
Chris@0
|
48 continue;
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 $entity_type_id = substr($plugin_id, 7);
|
Chris@0
|
52 $rest_post_route_name = "rest.entity.$entity_type_id.POST";
|
Chris@0
|
53 if ($rest_post_route = $route_collection->get($rest_post_route_name)) {
|
Chris@0
|
54 // Create a route for the 'create' link relation type for this entity
|
Chris@0
|
55 // type that uses the same route definition as the REST 'POST' route
|
Chris@0
|
56 // which use that entity type.
|
Chris@0
|
57 // @see \Drupal\Core\Entity\Entity::toUrl()
|
Chris@0
|
58 $entity_create_route_name = "entity.$entity_type_id.create";
|
Chris@0
|
59 $route_collection->add($entity_create_route_name, $rest_post_route);
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * {@inheritdoc}
|
Chris@0
|
66 */
|
Chris@0
|
67 public static function getSubscribedEvents() {
|
Chris@0
|
68 // Priority -10, to run after \Drupal\rest\Routing\ResourceRoutes, which has
|
Chris@0
|
69 // priority 0.
|
Chris@0
|
70 $events[RoutingEvents::DYNAMIC][] = ['onDynamicRouteEvent', -10];
|
Chris@0
|
71 return $events;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 }
|