Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. EntityResourcePostRouteSubscriber.php

function EntityResourcePostRouteSubscriber::onDynamicRouteEvent

Provides routes on route rebuild time.

Parameters

\Drupal\Core\Routing\RouteBuildEvent $event: The route build event.

File

core/modules/rest/src/EventSubscriber/EntityResourcePostRouteSubscriber.php, line 38

Class

EntityResourcePostRouteSubscriber
Generates a 'create' route for an entity type if it has a REST POST route.

Namespace

Drupal\rest\EventSubscriber

Code

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 (!str_starts_with($plugin_id, '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\EntityBase::toUrl()
            $entity_create_route_name = "entity.{$entity_type_id}.create";
            $route_collection->add($entity_create_route_name, $rest_post_route);
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal