You are here

protected function FileUploadResource::getUploadValidators in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/file/src/Plugin/rest/resource/FileUploadResource.php \Drupal\file\Plugin\rest\resource\FileUploadResource::getUploadValidators()

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 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 FileUploadResource::getUploadValidators()
FileUploadResource::post in core/modules/file/src/Plugin/rest/resource/FileUploadResource.php
Creates a file from an endpoint.

File

core/modules/file/src/Plugin/rest/resource/FileUploadResource.php, line 539

Class

FileUploadResource
File upload resource.

Namespace

Drupal\file\Plugin\rest\resource

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.
  $max_filesize = Bytes::toInt(Environment::getUploadMaxSize());
  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;
}