You are here

protected function TemporaryJsonapiFileFieldUploader::getUploadValidators in JSON:API 8.2

Retrieves the upload validators for a field definition.

This is copied from \Drupal\file\Plugin\Field\FieldType\FileItem as there is no entity instance available here that that a FileItem would exist for.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition for which to get validators.

Return value

array An array suitable for passing to file_save_upload() or the file field element's '#upload_validators' property.

1 call to TemporaryJsonapiFileFieldUploader::getUploadValidators()
TemporaryJsonapiFileFieldUploader::handleFileUploadForField in src/Controller/TemporaryJsonapiFileFieldUploader.php
Creates and validates a file entity for a file field from a file stream.

File

src/Controller/TemporaryJsonapiFileFieldUploader.php, line 459

Class

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

Namespace

Drupal\jsonapi\Controller

Code

protected function getUploadValidators(FieldDefinitionInterface $field_definition) {
  $validators = [
    // Add in our check of the file name length.
    'file_validate_name_length' => [],
  ];
  $settings = $field_definition
    ->getSettings();

  // Cap the upload size according to the PHP limit.
  // @todo Remove when we stop supporting Drupal 8.5 and 8.6.
  $max_bytes = floatval(\Drupal::VERSION) < 8.699999999999999 ? file_upload_max_size() : Environment::getUploadMaxSize();
  $max_filesize = Bytes::toInt($max_bytes);
  if (!empty($settings['max_filesize'])) {
    $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
  }

  // There is always a file size limit due to the PHP server limit.
  $validators['file_validate_size'] = [
    $max_filesize,
  ];

  // Add the extension check if necessary.
  if (!empty($settings['file_extensions'])) {
    $validators['file_validate_extensions'] = [
      $settings['file_extensions'],
    ];
  }
  return $validators;
}