protected function TemporaryJsonapiFileFieldUploader::getUploadValidators in Drupal 10
Same name and namespace in other branches
- 8 core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::getUploadValidators()
- 9 core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php \Drupal\jsonapi\Controller\TemporaryJsonapiFileFieldUploader::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.
File
- core/modules/ jsonapi/ src/ Controller/ TemporaryJsonapiFileFieldUploader.php, line 480 
Class
- TemporaryJsonapiFileFieldUploader
- Reads data from an upload stream and creates a corresponding file entity.
Namespace
Drupal\jsonapi\ControllerCode
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::toNumber(Environment::getUploadMaxSize());
  if (!empty($settings['max_filesize'])) {
    $max_filesize = min($max_filesize, Bytes::toNumber($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;
}