Problem/Motivation

Creating a site for a Cutstomer that wants to provide Parasolid files to his Partners.
those files have the extensions .x_t and .x_b
source: https://en.wikipedia.org/wiki/Parasolid
When trying to define the File Field I get:

The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.

Steps to reproduce

1. Ad a file field to a node
2. try to set the allowed Extensions to "x_t x_b"
3. try to save the Field settings

Proposed resolution

Allow '_' as a valid extension character

User interface text changes.

Allowed files extension help text:
Separate extensions with a comma or space. Each extension can contain alphanumeric characters, '.', and '_', and should start and end with an alphanumeric character.

Error message:
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.

Remaining tasks

None

User interface changes

Setting page for the file field, i.e. /admin/structure/types/manage/article/fields/node.article.field_file.

Before



After


API changes

afaik none

Data model changes

afaik none

Release notes snippet

Allow configuring file extensions with a underscore ie. x_t via the UI

CommentFileSizeAuthor
#60 interdiff_53_60.txt3.2 KBanmolgoyal74
#60 3166044-60.patch3.69 KBanmolgoyal74
#60 3166044-60-test-only.patch1.74 KBanmolgoyal74
#55 Before - Description.png16.71 KBanmolgoyal74
#55 After - description.png20.17 KBanmolgoyal74
#54 Before.png18.11 KBanmolgoyal74
#54 After.png24.5 KBanmolgoyal74
#53 interdiff_49_53.txt2.65 KBanmolgoyal74
#53 3166044-53.patch3.75 KBanmolgoyal74
#49 3166044-49.patch3.56 KBquietone
#49 diff-48-49.txt366 bytesquietone
#49 After-allowed-file-extension-help.png10.44 KBquietone
#49 After-error.png9.75 KBquietone
#48 interdiff_46_48.txt868 bytesanmolgoyal74
#48 3166044-48.patch3.55 KBanmolgoyal74
#46 interdiff_44_46.txt979 bytesanmolgoyal74
#46 3166044-46.patch3.53 KBanmolgoyal74
#44 interdiff_38_44.txt2.32 KBanmolgoyal74
#44 3166044-44.patch3.53 KBanmolgoyal74
#43 Before-allowed-extenstions.png9.27 KBquietone
#43 Before-error-msg.png7.42 KBquietone
#43 After-allowed-file-extension-help.png9.79 KBquietone
#43 AFter-file-settings.png7.77 KBquietone
#38 interdiff_35_38.txt2.88 KBanmolgoyal74
#38 3166044-38.patch3.43 KBanmolgoyal74
#35 interdiff_33_35.txt754 bytesanmolgoyal74
#35 3166044-35.patch1.98 KBanmolgoyal74
#33 interdiff_28-33.txt788 bytessarvjeetsingh
#33 3166044-33.patch1.98 KBsarvjeetsingh
#31 After--patch.gif3.48 MBranjith_kumar_k_u
#29 interdiff_9_28.txt845 bytesanmolgoyal74
#28 interdiff_9_28.patch845 bytesanmolgoyal74
#28 3166044-28.patch1.96 KBanmolgoyal74
#28 3166044-28-test-only.patch1.02 KBanmolgoyal74
#19 SettingsTrayBlockFormTest.png10.23 KBpaulocs
#15 After_Patch_Extension.png22.59 KBjanmejaig
#12 before_applying_Patch.png264.69 KBjanmejaig
#12 after_while_Uploading.png35.34 KBjanmejaig
#12 after_while_Saving.png22.5 KBjanmejaig
#9 3166044-9.patch1.96 KBidebr
#9 3166044-9-test-only.patch1.02 KBidebr
#2 Allow_File_extensions_containing_underscores.patch964 bytesspuky
Allow_File_extensions_containing_underscores.patch1007 bytesspuky
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

spuky created an issue. See original summary.

spuky’s picture

sorry wrong patch format...

spuky’s picture

spuky’s picture

Issue: https://www.drupal.org/project/drupal/issues/997900

Want's to provide a means for any File extension which I guess i critical.

But providing a way to allow file extensions that have a valid use case should be possible

spuky’s picture

Status: Active » Needs review

would be nice If some of you could look over this...

also maybe provide hints how and which test would need to be written.

larowlan’s picture

Category: Bug report » Feature request
Issue tags: +Bug Smash Initiative

Thanks, this is a feature request in my opinion.
In terms of testing, there should be an existing test case you can add to

spuky’s picture

Just because it is checking wrong since merging file module into D7 10 years ago... does not mean it is not a Bug ;-)

but if it helps the Bug Smash Initiative Stats to declare that as "not a bug" I am fine with that

The Validator in Question is just used in the fieldSettingsForm function of the Field module.

So it is preventing me as a User of the UI from configuring / changing File extensions that are totally valid when Importing them via configuration management... oder using apis to set them.

I am struggling finding a testcase that covers the use of this form validator or the fieldSettingsForm of file module that I could attach to. Can anybody provide guidance where to put test... and what kind of tests

spuky’s picture

idebr’s picture

#7 The classification for the category is explained in the issue handbook, see https://www.drupal.org/community/contributor-guide/reference-information...

Attached patch adds a test assertion a file extension containing an underscore can be configured through the user interface.

The last submitted patch, 9: 3166044-9-test-only.patch, failed testing. View results

spuky’s picture

Issue summary: View changes

Thanks for the Test hope some People will be looking over this to get it towards RTBC

janmejaig’s picture

After applying the patch it is allowing to save the "_" (extension) , but while uploading the same it is giving an issue . This is giving an issue while uploading the file with "_" extension. This is not full filling the expectation , please look at the screen shot after applying the patch.

janmejaig’s picture

Status: Needs review » Needs work
naresh_bavaskar’s picture

Status: Needs work » Reviewed & tested by the community

#9 LGTM and works fine for me for FIle field
Testing steps
1) Added x_t extension in article content type and.FIle field saved succesfully.
2) creating new node and uploaded "x_t" file into that file field and attached succesfully.
3) node saved successfully and again checked on edit page it remains attached file.

Just in checked patch that it has only functional test cases, not sure but might we should have more test cases on that.

@janmejaig I think you are trying to check _ (underscore) named extension in image field. but as per Issue summary this patch is allowing _ in file field.

janmejaig’s picture

FileSize
22.59 KB

I have recheck the above issue again and found that it is working fine for now . Please find the screen shot for the same .

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

spuky’s picture

Rerun patches from #9 against Drupal 9.2 and they are still valid... any more Input on this Issue by other People , what could be done better

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: 3166044-9.patch, failed testing. View results

paulocs’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
10.23 KB

Set back to RTBC because the failed test looks a random fail.

I run the test locally and did not find ant problem. See image attached.

jungle’s picture

Issue tags: +Needs change record

I think a CR is required, so tagging "Needs change record"

paulocs’s picture

Assigned: Unassigned » paulocs

I'll provide it.

paulocs’s picture

Assigned: paulocs » Unassigned
Issue tags: -Needs change record

I just create a CR. It is simple one but I think it is enough to understand. Please have a look.

jungle’s picture

Thanks, @paulocs.

BTW, Made a few little changes to it:

  • Selected Module developers under Impacts
  • Fixed a typo: exemple.x_t -> example.x_t
  • Changed the word File in title to use lowercase
jungle’s picture

Title: Allow File extensions containing underscores » Allow file extensions containing underscores

Changing the word File in title to use lowercase as well

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 9: 3166044-9.patch, failed testing. View results

jungle’s picture

Status: Needs work » Reviewed & tested by the community

An irrelevant random fail

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

There's at least one other file type with _ in the apache mime type file - application/vnd.denovo.fcselayout-link fe_launch.

+++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
@@ -227,7 +227,7 @@ public static function validateExtensions($element, FormStateInterface $form_sta
-      if (!preg_match('/^([a-z0-9]+([.][a-z0-9])* ?)+$/', $extensions)) {
+      if (!preg_match('/^([a-z0-9_]+([.][a-z0-9_])* ?)+$/', $extensions)) {

I think the regex should be ^([a-z0-9]+([._][a-z0-9])* ?)+$

I.e. I don't think we should allow _ and the beginning or the end and like a dot I think we should only allow one of them.

anmolgoyal74’s picture

Updated the regex as mentioned in #28

Tested with following extensions:
x_t, x.t, xt, x_y_t - accepted
x_.t, x._t, xt_, x__t, _xt - not accepted

anmolgoyal74’s picture

FileSize
845 bytes

The last submitted patch, 28: 3166044-28-test-only.patch, failed testing. View results

ranjith_kumar_k_u’s picture

FileSize
3.48 MB

The last patch works fine .
after patch

quietone’s picture

Status: Needs review » Needs work
+++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
@@ -160,6 +160,14 @@ public function testFileExtension() {
+    $this->drupalPostForm("admin/structure/types/manage/article/fields/node.article.$field_name", $edit, 'Save settings');

drupalPostForm is deprectated.

sarvjeetsingh’s picture

Status: Needs work » Needs review
FileSize
1.98 KB
788 bytes

Replacing the deprecated drupalPostForm method with the combination of drupalGet and submitForm methods.

Status: Needs review » Needs work

The last submitted patch, 33: 3166044-33.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
1.98 KB
754 bytes

Fixed the failing test.

jungle’s picture

Tested with following extensions:
x_t, x.t, xt, x_y_t - accepted
x_.t, x._t, xt_, x__t, _xt - not accepted

I think the above should be tested in the test(s).

quietone’s picture

Status: Needs review » Needs work

I was thinking the same thing. Back to NW for #36

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
3.43 KB
2.88 KB
quietone’s picture

Status: Needs review » Needs work
+++ b/core/modules/file/src/Plugin/Field/FieldType/FileItem.php
@@ -166,7 +166,7 @@
+      '#description' => t('Separate extensions with a space or comma and do not include the leading dot or underscores.'),

@@ -228,7 +228,7 @@
+        $form_state->setError($element, t('The list of allowed extensions is not valid, be sure to exclude leading dots or underscores and to separate extensions with a comma or space.'));

Since we are changing these lines can we also change t( to $this->t

Also, I think the messages are confusing and don't give me any understanding of what is allowed and not allow, particularly regarding underscores. In the first one, what does 'or underscores' mean. Does that mean that more than one underscore after the dot is not allowed? Is one underscore OK? Is '__a' accepted? Then the second message mentions 'leading dots' where as the first message is 'leading dot'. Can there be multiple dots? On yet another read, it is the addition of 'or underscores' that is causing problems for me.

I would say just revert these changes but then this is allowing underscores but only in certain positions in the extension and not two consecutive underscores. How to tell that to the user?

anmolgoyal74’s picture

@quietone Thanks for reviewing.
I knew this would come up. I look quite complex to explain all the possible values which can be added. Maybe we can show accepted and not accepted cases in the description or If you can provide eny other input.

quietone’s picture

@anmolgoyal74, Before I can think about the interface test I want to understand why there are limitations on the use of the underscore. Looking back I see it is from alexpott in #27.

I.e. I don't think we should allow _ and the beginning or the end and like a dot I think we should only allow one of them.

Why? For example, what is the problem with this filename?

$ ls -la f*
-rw-r--r-- 1 root root 4 Jan 15 21:07 foo.___
anmolgoyal74’s picture

@quietone
I have followed the suggestions from @alexpott in #27.
In my opinion, there is no problem with foo.___, but that is not a known extension. you can create a file with any extension like that.

$ ls -la fo*
-rw-r--r--  1 root       staff     4 Jan 18 22:43 foo._d:d@#$%6_3

I have the following document, I think we are good with the latest patch.
Check here https://en.wikipedia.org/wiki/List_of_file_formats.

quietone’s picture

@anmolgoyal74, thank you for the explanation. So, this just needs to have agreement on the user interface help text and to use $this->t.

So, the underscore is treated differently than [a-zA-z] and the dot. The file extensions can include underscores but not as the first or last character, multiple underscores are allowed in the extension but an underscore can not be followed by an underscore.

I am not good at user interface text and think this needs input from the UX folks. I considered setting to RTBC and having a follow up for the text but the text as it is now is confusing, at least to me, so I rejected that idea.

I have updated the IS and added some screenshots showing the changed text.

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
3.53 KB
2.32 KB

Change t() to this->t().
And also tried to update the description.

Status: Needs review » Needs work

The last submitted patch, 44: 3166044-44.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
3.53 KB
979 bytes

Status: Needs review » Needs work

The last submitted patch, 46: 3166044-46.patch, failed testing. View results

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
3.55 KB
868 bytes

Updated tests.

quietone’s picture

I talked with benjifisher who suggested that this gets tagged, 'Needs usability review', and to post in #ux or in the agenda for the next Usability meeting. So adding tag and I'll do the others too.

Updated the IS with the latest text and screen shots, which required a patch reroll. :-(

quietone’s picture

I got a message from jhodgson on #ux that the best way to get this reviewed at the ux meeting is for someone to show up and explain what is needed. The next meeting is Friday 15:00 UTC. I really don't want to attend because that is Sat 0400 for me. Can anyone here attend and get the interface text discussed?

benjifisher’s picture

Looking at the patch from #49, the existing help text is

Separate extensions with a space or comma and do not include the leading dot.

and the proposed help text is

Separate extensions with a space or comma and should start & end with an alphanumeric character and can contain a dot or an underscore but not together.

If the validation fails, the original error message is

The list of allowed extensions is not valid, be sure to exclude leading dots and to separate extensions with a comma or space.

and the proposed error message is

The list of allowed extensions is not valid, be sure to exclude leading or consecutive dots & underscores and to separate extensions with a comma or space.

I think having the text like this, as text, makes it easier to review than having it in a screenshot. That is especially true for people who are visually impaired.

benjifisher’s picture

Status: Needs review » Needs work
Issue tags: -Needs usability review +Needs issue summary update

Usability review

We discussed this issue at #3194390: Drupal Usability Meeting 2021-01-29.

General rules for interface text include

  • Help text should be kept short.
  • Keep sentences short.
  • Avoid contractions and abbreviations.

Some of this is specified on Interface text. It does not mention "&" vs. "and", but I think it should.

Following these guidelines, we suggest the following for the help text:

Separate extensions with a comma or space. Each extension can contain alphanumeric characters, '.', and '_', and should start and end with an alphanumeric character.

In order to keep it short, we leave out the rule that '.' and '_' cannot appear next to each other. Although it is annoying to be surprised by such a rule after submitting the form, we want to keep the text short and it seems unlikely to come up.

The error message can be a little longer than the help text, and it should be more explicit. We suggest

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.

I am setting the status to NW for the text changes and an update to the issue summary. Please add the text changes to the "Proposed resolution" section and update the screenshots in the "User interface changes" section.

anmolgoyal74’s picture

Status: Needs work » Needs review
FileSize
3.75 KB
2.65 KB

Updated the text as per the proposed solution.

anmolgoyal74’s picture

FileSize
24.5 KB
18.11 KB
anmolgoyal74’s picture

anmolgoyal74’s picture

Issue summary: View changes
anmolgoyal74’s picture

Issue summary: View changes
quietone’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update

@benjifisher, thank you for the UX review. I hope to remember to show the strings in the future instead of screenshots.

#52. Added the string changes to the Proposed Resolution in the IS. The screen shots were updated in #55

I applied the patch and tested with valid and invalid field extensions, 'a.b', 'a..', 'a__'. With invalid options I found that the error message gave enough information so I could fix the problem. And the help text was clear and concise.

I then confirmed that the text changes exactly matched the suggested changes. (Probably should have done this first)

Both invalid and valid extensions are tested so I reckon this is ready.

@anmolgoyal74, thanks for your commitment to getting this in.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

This all looks really good. A recent fix has added testing of very similar functionality...

+++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php
@@ -160,6 +160,27 @@ public function testFileExtension() {
+    // 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/article/fields/node.article.$field_name");
+    $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/article/fields/node.article.$field_name");
+      $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.");
+    }

Let's part this part of \Drupal\Tests\file\Functional\FileFieldWidgetTest::testFileExtensionsSetting() as this is about configuring the file field and it test very similar functionality.

anmolgoyal74’s picture

Move the test from +++ b/core/modules/file/tests/src/Functional/FileFieldValidateTest.php to +++ b/core/modules/file/tests/src/Functional/FileFieldWidgetTest.php.

The last submitted patch, 60: 3166044-60-test-only.patch, failed testing. View results

quietone’s picture

Status: Needs review » Reviewed & tested by the community

Yes, that makes the change requested in #59.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 663c57b and pushed to 9.2.x. Thanks!

  • alexpott committed 663c57b on 9.2.x
    Issue #3166044 by anmolgoyal74, spuky, idebr, quietone, sarvjeetsingh,...
jungle’s picture

BTW, updated CR and published it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.