$violations =
$file;
$message = "Unprocessable Entity: file validation failed.\n";
$message .=
implode("\n",
array_map(function DConstraintViolationInterface
$violation) { return PlainTextOutput::
renderFromHtml($violation->
getMessage());
},
iterator_to_array($violations)));
throw new UnprocessableEntityHttpException($message);
} // @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,
[]);
} /**
* Ensures that the given account is allowed to upload a file.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The account for which access should be checked.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field for which the file is to be uploaded.
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
* The entity, if one exists, for which the file is to be uploaded.
*/