function FileUpload::handleFileUploadForNewResource

Same name in other branches
  1. 9 core/modules/jsonapi/src/Controller/FileUpload.php \Drupal\jsonapi\Controller\FileUpload::handleFileUploadForNewResource()
  2. 8.9.x core/modules/jsonapi/src/Controller/FileUpload.php \Drupal\jsonapi\Controller\FileUpload::handleFileUploadForNewResource()
  3. 11.x core/modules/jsonapi/src/Controller/FileUpload.php \Drupal\jsonapi\Controller\FileUpload::handleFileUploadForNewResource()

Handles JSON:API file upload requests.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The HTTP request object.

\Drupal\jsonapi\ResourceType\ResourceType $resource_type: The JSON:API resource type for the current request.

string $file_field_name: The file field for which the file is to be uploaded.

Return value

\Drupal\jsonapi\ResourceResponse The response object.

Throws

\Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException Thrown when there are validation errors.

File

core/modules/jsonapi/src/Controller/FileUpload.php, line 158

Class

FileUpload
Handles file upload requests.

Namespace

Drupal\jsonapi\Controller

Code

public function handleFileUploadForNewResource(Request $request, ResourceType $resource_type, string $file_field_name) : ResourceResponse {
    $result = $this->handleFileUploadForResource($request, $resource_type, $file_field_name);
    $file = $result->getFile();
    // @todo Remove line below in favor of commented line in https://www.drupal.org/project/drupal/issues/2878463.
    $self_link = new Link(new CacheableMetadata(), Url::fromRoute('jsonapi.file--file.individual', [
        'entity' => $file->uuid(),
    ]), 'self');
    
    /* $self_link = new Link(new CacheableMetadata(), $this->entity->toUrl('jsonapi'), ['self']); */
    $links = new LinkCollection([
        'self' => $self_link,
    ]);
    $relatable_resource_types = $resource_type->getRelatableResourceTypesByField($resource_type->getPublicName($file_field_name));
    $file_resource_type = reset($relatable_resource_types);
    $resource_object = ResourceObject::createFromEntity($file_resource_type, $file);
    return new ResourceResponse(new JsonApiDocumentTopLevel(new ResourceObjectData([
        $resource_object,
    ], 1), new NullIncludedData(), $links), 201, []);
}

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