You are here

protected static function Routes::getRoutesForResourceType in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Routing/Routes.php \Drupal\jsonapi\Routing\Routes::getRoutesForResourceType()
  2. 10 core/modules/jsonapi/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 core/modules/jsonapi/src/Routing/Routes.php

File

core/modules/jsonapi/src/Routing/Routes.php, line 151

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`.
  if ($resource_type
    ->isLocatable()) {
    $collection_route = new Route("/{$resource_type->getPath()}");
    $collection_route
      ->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_SERVICE_NAME . ':getCollection',
    ]);
    $collection_route
      ->setMethods([
      'GET',
    ]);

    // Allow anybody access because "view" and "view label" access are checked
    // in the controller.
    $collection_route
      ->setRequirement('_access', 'TRUE');
    $routes
      ->add(static::getRouteName($resource_type, 'collection'), $collection_route);
  }

  // Creation route.
  if ($resource_type
    ->isMutable()) {
    $collection_create_route = new Route("/{$resource_type->getPath()}");
    $collection_create_route
      ->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_SERVICE_NAME . ':createIndividual',
    ]);
    $collection_create_route
      ->setMethods([
      'POST',
    ]);
    $create_requirement = sprintf("%s:%s", $resource_type
      ->getEntityTypeId(), $resource_type
      ->getBundle());
    $collection_create_route
      ->setRequirement('_entity_create_access', $create_requirement);
    $collection_create_route
      ->setRequirement('_csrf_request_header_token', 'TRUE');
    $routes
      ->add(static::getRouteName($resource_type, 'collection.post'), $collection_create_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 base path.
  $routes
    ->addPrefix($path_prefix);
  return $routes;
}