You are here

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

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

Gets applicable resource routes for a JSON API resource type.

Parameters

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON API resource type for which to get the routes.

string $path_prefix: The root path prefix.

Return value

\Symfony\Component\Routing\RouteCollection A collection of routes for the given resource type.

1 call to Routes::getRoutesForResourceType()
Routes::routes in src/Routing/Routes.php

File

src/Routing/Routes.php, line 127

Class

Routes
Defines dynamic routes.

Namespace

Drupal\jsonapi\Routing

Code

protected static function getRoutesForResourceType(ResourceType $resource_type, $path_prefix) {

  // Internal resources have no routes.
  if ($resource_type
    ->isInternal()) {
    return new RouteCollection();
  }
  $routes = new RouteCollection();

  // Collection route like `/jsonapi/node/article`.
  $collection_route = new Route('/' . $resource_type
    ->getPath());
  $collection_route
    ->setMethods($resource_type
    ->isLocatable() ? [
    'GET',
    'POST',
  ] : [
    'POST',
  ]);
  $collection_route
    ->addDefaults([
    'serialization_class' => JsonApiDocumentTopLevel::class,
  ]);
  $collection_route
    ->setRequirement('_csrf_request_header_token', 'TRUE');
  $routes
    ->add(static::getRouteName($resource_type, 'collection'), $collection_route);

  // Individual routes like `/jsonapi/node/article/{uuid}` or
  // `/jsonapi/node/article/{uuid}/relationships/uid`.
  $routes
    ->addCollection(static::getIndividualRoutesForResourceType($resource_type));

  // Add the resource type as a parameter to every resource route.
  foreach ($routes as $route) {
    static::addRouteParameter($route, static::RESOURCE_TYPE_KEY, [
      'type' => ResourceTypeConverter::PARAM_TYPE_ID,
    ]);
    $route
      ->addDefaults([
      static::RESOURCE_TYPE_KEY => $resource_type
        ->getTypeName(),
    ]);
  }

  // Resource routes all have the same controller.
  $routes
    ->addDefaults([
    RouteObjectInterface::CONTROLLER_NAME => static::FRONT_CONTROLLER,
  ]);
  $routes
    ->addRequirements([
    '_jsonapi_custom_query_parameter_names' => 'TRUE',
  ]);
  $routes
    ->addPrefix($path_prefix);
  return $routes;
}