You are here

protected function TemporaryJsonapiFileFieldUploader::validate in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::validate()
  2. 9 core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::validate()

Validates the file.

@todo this method is unused in this class because file validation needs to be split up in 2 steps in ::handleFileUploadForField(). Add a deprecation notice as soon as a central core file upload service can be used in this class. See https://www.drupal.org/project/drupal/issues/2940383

Parameters

\Drupal\file\FileInterface $file: The file entity to validate.

array $validators: An array of upload validators to pass to file_validate().

Return value

\Drupal\Core\Entity\EntityConstraintViolationListInterface The list of constraint violations, if any.

File

core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php, line 402

Class

TemporaryJsonapiFileFieldUploader
Reads data from an upload stream and creates a corresponding file entity.

Namespace

Drupal\jsonapi\Controller

Code

protected function validate(FileInterface $file, array $validators) {
  $violations = $file
    ->validate();

  // Remove violations of inaccessible fields as they cannot stem from our
  // changes.
  $violations
    ->filterByFieldAccess();

  // Validate the file based on the field definition configuration.
  $errors = file_validate($file, $validators);
  if (!empty($errors)) {
    $translator = new DrupalTranslator();
    foreach ($errors as $error) {
      $violation = new ConstraintViolation($translator
        ->trans($error), $error, [], EntityAdapter::createFromEntity($file), '', NULL);
      $violations
        ->add($violation);
    }
  }
  return $violations;
}