function Routes::getFileUploadRoutesForResourceType

Gets the file upload route collection for the given resource type.

Parameters

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

string $path_prefix: The root path prefix.

Return value

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

1 call to Routes::getFileUploadRoutesForResourceType()
Routes::routes in core/modules/jsonapi/src/Routing/Routes.php

File

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

Class

Routes
Defines dynamic routes.

Namespace

Drupal\jsonapi\Routing

Code

protected static function getFileUploadRoutesForResourceType(ResourceType $resource_type, $path_prefix) {
  $routes = new RouteCollection();
  // Internal resources have no routes; individual routes require locations.
  if ($resource_type->isInternal() || !$resource_type->isLocatable()) {
    return $routes;
  }
  // File upload routes are only necessary for resource types that have file
  // fields.
  $has_file_field = array_reduce($resource_type->getRelatableResourceTypes(), function ($carry, array $target_resource_types) {
    return $carry || static::hasNonInternalFileTargetResourceTypes($target_resource_types);
  }, FALSE);
  if (!$has_file_field) {
    return $routes;
  }
  if ($resource_type->isMutable()) {
    $path = $resource_type->getPath();
    $entity_type_id = $resource_type->getEntityTypeId();
    $new_resource_file_upload_route = new Route("/{$path}/{file_field_name}");
    $new_resource_file_upload_route->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => 'jsonapi.file_upload:handleFileUploadForNewResource',
    ]);
    $new_resource_file_upload_route->setMethods([
      'POST',
    ]);
    $new_resource_file_upload_route->setRequirement('_csrf_request_header_token', 'TRUE');
    $routes->add(static::getFileUploadRouteName($resource_type, 'new_resource'), $new_resource_file_upload_route);
    $existing_resource_file_upload_route = new Route("/{$path}/{entity}/{file_field_name}");
    $existing_resource_file_upload_route->addDefaults([
      RouteObjectInterface::CONTROLLER_NAME => 'jsonapi.file_upload:handleFileUploadForExistingResource',
    ]);
    $existing_resource_file_upload_route->setMethods([
      'POST',
    ]);
    $existing_resource_file_upload_route->setRequirement('_csrf_request_header_token', 'TRUE');
    $routes->add(static::getFileUploadRouteName($resource_type, 'existing_resource'), $existing_resource_file_upload_route);
    // Add entity parameter conversion to every route.
    $routes->addOptions([
      'parameters' => [
        'entity' => [
          'type' => 'entity:' . $entity_type_id,
        ],
      ],
    ]);
    // 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(),
      ]);
    }
  }
  // File upload routes all have the same base path.
  $routes->addPrefix($path_prefix);
  return $routes;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.