function FileHooks::formSystemFileSystemSettingsAlter

Implements hook_form_FORM_ID_alter().

Injects the file sanitization options into /admin/config/media/file-system.

These settings are enforced during upload by the FileEventSubscriber that listens to the FileUploadSanitizeNameEvent event.

See also

\Drupal\system\Form\FileSystemForm

\Drupal\Core\File\Event\FileUploadSanitizeNameEvent

\Drupal\file\EventSubscriber\FileEventSubscriber

File

core/modules/file/src/Hook/FileHooks.php, line 352

Class

FileHooks
Hook implementations for file.

Namespace

Drupal\file\Hook

Code

public function formSystemFileSystemSettingsAlter(array &$form, FormStateInterface $form_state) : void {
    $config = \Drupal::config('file.settings');
    $form['filename_sanitization'] = [
        '#type' => 'details',
        '#title' => t('Sanitize filenames'),
        '#description' => t('These settings only apply to new files as they are uploaded. Changes here do not affect existing file names.'),
        '#open' => TRUE,
        '#tree' => TRUE,
    ];
    $form['filename_sanitization']['replacement_character'] = [
        '#type' => 'select',
        '#title' => t('Replacement character'),
        '#default_value' => $config->get('filename_sanitization.replacement_character'),
        '#options' => [
            '-' => t('Dash (-)'),
            '_' => t('Underscore (_)'),
        ],
        '#description' => t('Used when replacing whitespace, replacing non-alphanumeric characters or transliterating unknown characters.'),
    ];
    $form['filename_sanitization']['transliterate'] = [
        '#type' => 'checkbox',
        '#title' => t('Transliterate'),
        '#default_value' => $config->get('filename_sanitization.transliterate'),
        '#description' => t('Transliteration replaces any characters that are not alphanumeric, underscores, periods or hyphens with the replacement character. It ensures filenames only contain ASCII characters. It is recommended to keep transliteration enabled.'),
    ];
    $form['filename_sanitization']['replace_whitespace'] = [
        '#type' => 'checkbox',
        '#title' => t('Replace whitespace with the replacement character'),
        '#default_value' => $config->get('filename_sanitization.replace_whitespace'),
    ];
    $form['filename_sanitization']['replace_non_alphanumeric'] = [
        '#type' => 'checkbox',
        '#title' => t('Replace non-alphanumeric characters with the replacement character'),
        '#default_value' => $config->get('filename_sanitization.replace_non_alphanumeric'),
        '#description' => t('Alphanumeric characters, dots <span aria-hidden="true">(.)</span>, underscores <span aria-hidden="true">(_)</span> and dashes <span aria-hidden="true">(-)</span> are preserved.'),
    ];
    $form['filename_sanitization']['deduplicate_separators'] = [
        '#type' => 'checkbox',
        '#title' => t('Replace sequences of dots, underscores and/or dashes with the replacement character'),
        '#default_value' => $config->get('filename_sanitization.deduplicate_separators'),
    ];
    $form['filename_sanitization']['lowercase'] = [
        '#type' => 'checkbox',
        '#title' => t('Convert to lowercase'),
        '#default_value' => $config->get('filename_sanitization.lowercase'),
    ];
    $form['#submit'][] = 'file_system_settings_submit';
}

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