Skip to content
Snippets Groups Projects
Unverified Commit 663c57bf authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3166044 by anmolgoyal74, spuky, idebr, quietone, sarvjeetsingh,...

Issue #3166044 by anmolgoyal74, spuky, idebr, quietone, sarvjeetsingh, janmejaig, paulocs, ranjith_kumar_k_u, jungle, benjifisher, alexpott: Allow file extensions containing underscores
parent 216dc5c9
No related branches found
No related tags found
No related merge requests found
......@@ -166,7 +166,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => t('Allowed file extensions'),
'#default_value' => $extensions,
'#description' => t('Separate extensions with a space or comma and do not include the leading dot.'),
'#description' => $this->t("Separate extensions with a comma or space. Each extension can contain alphanumeric characters, '.', and '_', and should start and end with an alphanumeric character."),
'#element_validate' => [[static::class, 'validateExtensions']],
'#weight' => 1,
'#maxlength' => 256,
......@@ -227,8 +227,8 @@ public static function validateExtensions($element, FormStateInterface $form_sta
$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, be sure to exclude leading dots and to separate extensions with a comma or space.'));
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);
......
......@@ -501,6 +501,27 @@ public function testFileExtensionsSetting() {
$edit = ['settings[file_extensions]' => 'jpg php'];
$this->submitForm($edit, 'Save settings');
$this->assertSession()->pageTextContains('Saved ' . $field_name . ' configuration.');
// Check that a file extension with an underscore can be configured.
$edit = [
'settings[file_extensions]' => 'x_t x.t xt x_y_t',
];
$this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_id");
$this->submitForm($edit, 'Save settings');
$field = FieldConfig::loadByName('node', $type_name, $field_name);
$this->assertEquals('x_t x.t xt x_y_t', $field->getSetting('file_extensions'));
// Check that a file field with an invalid value in allowed extensions
// property throws an error message.
$invalid_extensions = ['x_.t', 'x._t', 'xt_', 'x__t', '_xt'];
foreach ($invalid_extensions as $value) {
$edit = [
'settings[file_extensions]' => $value,
];
$this->drupalGet("admin/structure/types/manage/$type_name/fields/$field_id");
$this->submitForm($edit, 'Save settings');
$this->assertSession()->pageTextContains("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.");
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment