public static function FileItem::validateExtensions in Drupal 10
Same name and namespace in other branches
- 8 core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions()
- 9 core/modules/file/src/Plugin/Field/FieldType/FileItem.php \Drupal\file\Plugin\Field\FieldType\FileItem::validateExtensions()
Form API callback.
This function is assigned as an #element_validate callback in fieldSettingsForm().
This doubles as a convenience clean-up function and a validation routine. Commas are allowed by the end-user, but ultimately the value will be stored as a space-separated list for compatibility with file_validate_extensions().
File
- core/
modules/ file/ src/ Plugin/ Field/ FieldType/ FileItem.php, line 225
Class
- FileItem
- Plugin implementation of the 'file' field type.
Namespace
Drupal\file\Plugin\Field\FieldTypeCode
public static function validateExtensions($element, FormStateInterface $form_state) {
if (!empty($element['#value'])) {
$extensions = preg_replace('/([, ]+\\.?)/', ' ', trim(strtolower($element['#value'])));
$extension_array = array_unique(array_filter(explode(' ', $extensions)));
$extensions = implode(' ', $extension_array);
if (!preg_match('/^([a-z0-9]+([._][a-z0-9])* ?)+$/', $extensions)) {
$form_state
->setError($element, t("The list of allowed extensions is not valid. Allowed characters are a-z, 0-9, '.', and '_'. The first and last characters cannot be '.' or '_', and these two characters cannot appear next to each other. Separate extensions with a comma or space."));
}
else {
$form_state
->setValueForElement($element, $extensions);
}
// If insecure uploads are not allowed and txt is not in the list of
// allowed extensions, ensure that no insecure extensions are allowed.
if (!in_array('txt', $extension_array, TRUE) && !\Drupal::config('system.file')
->get('allow_insecure_uploads')) {
foreach ($extension_array as $extension) {
if (preg_match(FileSystemInterface::INSECURE_EXTENSION_REGEX, 'test.' . $extension)) {
$form_state
->setError($element, t('Add %txt_extension to the list of allowed extensions to securely upload files with a %extension extension. The %txt_extension extension will then be added automatically.', [
'%extension' => $extension,
'%txt_extension' => 'txt',
]));
break;
}
}
}
}
}