X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Frest%2Fsrc%2FEventSubscriber%2FEntityResourcePostRouteSubscriber.php;fp=web%2Fcore%2Fmodules%2Frest%2Fsrc%2FEventSubscriber%2FEntityResourcePostRouteSubscriber.php;h=36a3f7311709f3da641a93a7ba27018638c2ea71;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/rest/src/EventSubscriber/EntityResourcePostRouteSubscriber.php b/web/core/modules/rest/src/EventSubscriber/EntityResourcePostRouteSubscriber.php new file mode 100644 index 000000000..36a3f7311 --- /dev/null +++ b/web/core/modules/rest/src/EventSubscriber/EntityResourcePostRouteSubscriber.php @@ -0,0 +1,74 @@ +resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_config'); + } + + /** + * 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(); + + $resource_configs = $this->resourceConfigStorage->loadMultiple(); + // Iterate over all REST resource config entities. + foreach ($resource_configs as $resource_config) { + // We only care about REST resource config entities for the + // \Drupal\rest\Plugin\rest\resource\EntityResource plugin. + $plugin_id = $resource_config->toArray()['plugin_id']; + if (substr($plugin_id, 0, 6) !== 'entity') { + continue; + } + + $entity_type_id = substr($plugin_id, 7); + $rest_post_route_name = "rest.entity.$entity_type_id.POST"; + if ($rest_post_route = $route_collection->get($rest_post_route_name)) { + // Create a route for the 'create' link relation type for this entity + // type that uses the same route definition as the REST 'POST' route + // which use that entity type. + // @see \Drupal\Core\Entity\Entity::toUrl() + $entity_create_route_name = "entity.$entity_type_id.create"; + $route_collection->add($entity_create_route_name, $rest_post_route); + } + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + // Priority -10, to run after \Drupal\rest\Routing\ResourceRoutes, which has + // priority 0. + $events[RoutingEvents::DYNAMIC][] = ['onDynamicRouteEvent', -10]; + return $events; + } + +}