function FileUploadHandler::handleExtensionValidation

Same name in other branches
  1. 9 core/modules/file/src/Upload/FileUploadHandler.php \Drupal\file\Upload\FileUploadHandler::handleExtensionValidation()
  2. 10 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 320

Class

FileUploadHandler
Handles validating and creating file entities from file uploads.

Namespace

Drupal\file\Upload

Code

protected function handleExtensionValidation(array &$validators) : string {
    // 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.