You are here

protected function FileUpload::getUploadValidators in GraphQL 8.4

Retrieves the upload validators for the given file field settings.

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

array $settings: The file field settings.

Return value

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

1 call to FileUpload::getUploadValidators()
FileUpload::saveFileUpload in src/GraphQL/Utility/FileUpload.php
Validates an uploaded file, saves it and returns a file upload response.

File

src/GraphQL/Utility/FileUpload.php, line 457

Class

FileUpload
Service to manage file uploads within GraphQL mutations.

Namespace

Drupal\graphql\GraphQL\Utility

Code

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

  // 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;
}