You are here

protected static function Routes::getIndividualRoutesForResourceType in JSON:API 8

Same name and namespace in other branches
  1. 8.2 src/Routing/Routes.php \Drupal\jsonapi\Routing\Routes::getIndividualRoutesForResourceType()

A collection route for the given resource type.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The resource type for which the route collection should be created.

Return value

\Symfony\Component\Routing\RouteCollection The route collection.

1 call to Routes::getIndividualRoutesForResourceType()
Routes::getRoutesForResourceType in src/Routing/Routes.php
Gets applicable resource routes for a JSON API resource type.

File

src/Routing/Routes.php, line 169

Class

Routes
Defines dynamic routes.

Namespace

Drupal\jsonapi\Routing

Code

protected static function getIndividualRoutesForResourceType(ResourceType $resource_type) {
  if (!$resource_type
    ->isLocatable()) {
    return new RouteCollection();
  }
  $routes = new RouteCollection();
  $path = $resource_type
    ->getPath();
  $entity_type_id = $resource_type
    ->getEntityTypeId();

  // Individual read, update and remove.
  $individual_route = new Route("/{$path}/{{$entity_type_id}}");
  $individual_route
    ->setMethods([
    'GET',
    'PATCH',
    'DELETE',
  ]);
  $individual_route
    ->addDefaults([
    'serialization_class' => JsonApiDocumentTopLevel::class,
  ]);
  $individual_route
    ->setRequirement('_csrf_request_header_token', 'TRUE');
  $routes
    ->add(static::getRouteName($resource_type, 'individual'), $individual_route);

  // Get an individual resource's related resources.
  $related_route = new Route("/{$path}/{{$entity_type_id}}/{related}");
  $related_route
    ->setMethods([
    'GET',
  ]);
  $routes
    ->add(static::getRouteName($resource_type, 'related'), $related_route);

  // Read, update, add, or remove an individual resources relationships to
  // other resources.
  $relationship_route = new Route("/{$path}/{{$entity_type_id}}/relationships/{related}");
  $relationship_route
    ->setMethods([
    'GET',
    'POST',
    'PATCH',
    'DELETE',
  ]);

  // @todo: remove the _on_relationship default in https://www.drupal.org/project/jsonapi/issues/2953346.
  $relationship_route
    ->addDefaults([
    '_on_relationship' => TRUE,
  ]);
  $relationship_route
    ->addDefaults([
    'serialization_class' => EntityReferenceFieldItemList::class,
  ]);
  $relationship_route
    ->setRequirement('_csrf_request_header_token', 'TRUE');
  $routes
    ->add(static::getRouteName($resource_type, 'relationship'), $relationship_route);

  // Add entity parameter conversion to every route.
  $routes
    ->addOptions([
    'parameters' => [
      $entity_type_id => [
        'type' => 'entity:' . $entity_type_id,
      ],
    ],
  ]);
  return $routes;
}