function TemporaryJsonapiFileFieldUploader::getUploadValidators

Same name in other branches
  1. 8.9.x 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.

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

File

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

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.
    $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.