function FileValidatorSettingsTrait::getFileUploadValidators

Same name in other branches
  1. 10 core/modules/file/src/Validation/FileValidatorSettingsTrait.php \Drupal\file\Validation\FileValidatorSettingsTrait::getFileUploadValidators()

Gets the upload validators for the specified settings.

Parameters

array $settings: An associative array of settings. The following keys are supported:

  • max_filesize: The maximum file size in bytes. Defaults to the PHP max

upload size.

  • file_extensions: A space-separated list of allowed file extensions.

Return value

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

4 calls to FileValidatorSettingsTrait::getFileUploadValidators()
FileItem::getUploadValidators in core/modules/file/src/Plugin/Field/FieldType/FileItem.php
Retrieves the upload validators for a file field.
FileUpload::handleFileUploadForResource in core/modules/jsonapi/src/Controller/FileUpload.php
Handles JSON:API file upload requests.
FileUploadResource::post in core/modules/file/src/Plugin/rest/resource/FileUploadResource.php
Creates a file from an endpoint.
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/file/src/Validation/FileValidatorSettingsTrait.php, line 26

Class

FileValidatorSettingsTrait
Provides a trait to create validators from settings.

Namespace

Drupal\file\Validation

Code

public function getFileUploadValidators(array $settings) : array {
    $validators = [
        // Add in our check of the file name length.
'FileNameLength' => [],
    ];
    // Cap the upload size according to the PHP limit.
    $maxFilesize = Bytes::toNumber(Environment::getUploadMaxSize());
    if (!empty($settings['max_filesize'])) {
        $maxFilesize = min($maxFilesize, Bytes::toNumber($settings['max_filesize']));
    }
    // There is always a file size limit due to the PHP server limit.
    $validators['FileSizeLimit'] = [
        'fileLimit' => $maxFilesize,
    ];
    // Add the extension check if necessary.
    if (!empty($settings['file_extensions'])) {
        $validators['FileExtension'] = [
            'extensions' => $settings['file_extensions'],
        ];
    }
    return $validators;
}

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