function FileUploadHandler::handleExtensionValidation

Same name in other branches
  1. 9 core/modules/file/src/Upload/FileUploadHandler.php \Drupal\file\Upload\FileUploadHandler::handleExtensionValidation()
  2. 11.x core/modules/file/src/Upload/FileUploadHandler.php \Drupal\file\Upload\FileUploadHandler::handleExtensionValidation()

Gets the list of allowed extensions and updates the validators.

This will add an extension validator to the list of validators if one is not set.

If the extension validator is set, but no extensions are specified, it means all extensions are allowed, so the validator is removed from the list of validators.

Parameters

array $validators: The file validators in use.

Return value

string The space delimited list of allowed file extensions.

1 call to FileUploadHandler::handleExtensionValidation()
FileUploadHandler::handleFileUpload in core/modules/file/src/Upload/FileUploadHandler.php
Creates a file from an upload.

File

core/modules/file/src/Upload/FileUploadHandler.php, line 414

Class

FileUploadHandler
Handles validating and creating file entities from file uploads.

Namespace

Drupal\file\Upload

Code

protected function handleExtensionValidation(array &$validators) : string {
    // Handle legacy extension validation.
    if (isset($validators['file_validate_extensions'])) {
        @trigger_error('\'file_validate_extensions\' is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'FileExtension\' constraint instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
        // Empty string means all extensions are allowed so we should remove the
        // validator.
        if (\is_string($validators['file_validate_extensions']) && empty($validators['file_validate_extensions'])) {
            unset($validators['file_validate_extensions']);
            return '';
        }
        // The deprecated 'file_validate_extensions' has configuration, so that
        // should be used.
        $validators['FileExtension']['extensions'] = $validators['file_validate_extensions'][0];
        unset($validators['file_validate_extensions']);
        return $validators['FileExtension']['extensions'];
    }
    // No validator was provided, so add one using the default list.
    // Build a default non-munged safe list for
    // \Drupal\system\EventSubscriber\SecurityFileUploadEventSubscriber::sanitizeName().
    if (!isset($validators['FileExtension'])) {
        $validators['FileExtension'] = [
            'extensions' => self::DEFAULT_EXTENSIONS,
        ];
        return self::DEFAULT_EXTENSIONS;
    }
    // Check if we want to allow all extensions.
    if (!isset($validators['FileExtension']['extensions'])) {
        // If 'FileExtension' is set and the list is empty then the caller wants
        // to allow any extension. In this case we have to remove the validator
        // or else it will reject all extensions.
        unset($validators['FileExtension']);
        return '';
    }
    return $validators['FileExtension']['extensions'];
}

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