Problem/Motivation
Issue summary is based on original report by @malc0mn
The setting of the Files displayed by default checkbox in the file widget settings is not respected. The visibility is always on, no matter how you set it up.
Steps to reproduce
1. Create a file field for a node.
2. Select widget 'file'
3. In the settings, tick the Enable Display field checkbox
4. Tick the Files displayed by default checkbox
5. Save
6. Create a new node of the type to which you attached the file field
7. Upload a file to the field
8. Note the state of the checkbox in the DISPLAY column: it is unticked even though we asked for it not to be so.
Proposed resolution
While writing test cases here we noticed that issue seems to be in claro theme.
So overall solution is:
- Set the display flag in the field widget so other themes can use it
- Use that flag in claro theme's "_claro_preprocess_file_and_image_widget" method to set the correct checkbox values
Remaining tasks
Needs review
User interface changes
Before

after

API changes
N/A
Data model changes
N/A
Release notes snippet
N/A
| Comment | File | Size | Author |
|---|---|---|---|
| #63 | interdiff-2272637-57-63.txt | 2.81 KB | ashley_herodev |
| #63 | 2272637-63.patch | 7 KB | ashley_herodev |
| #55 | Screenshot 2023-10-03_before.png | 21.02 KB | ashley_herodev |
| #55 | interdiff-2272637-47-55.txt | 3.78 KB | ashley_herodev |
| #55 | 2272637-55.patch | 63 bytes | ashley_herodev |
Comments
Comment #1
malc0mn commentedAnd here is the patch...
Cheers,
mlc.
Comment #3
malc0mn commentedHmm, crap... I can see the problem from the tests...
But how to fix it properly then? Because it IS a bug IMHO...
mlc.
Comment #4
malc0mn commentedAttempt 2...
mlc.
Comment #6
malc0mn commented4: default_file_visibility_setting_not_respected-file_display-2272637-4-7.x.patch queued for re-testing.
Comment #7
malc0mn commentedComment #8
dcam commentedThis affects 8.x and will have to be fixed there first. See process() in /core/modules/file/src/Plugin/Field/FieldWidget.php.
I'm not at all familiar with how this widget works, but I'm guessing that this hidden field in the else statement is a no-JS fallback to make sure that the display value is populated when the file's database record is created.
malc0mn is correct that the widget doesn't respect the field setting.
The most obvious problem is that this always-true value prevents the AJAX-rebuilt form from using the field setting.
A second problem is that if a user has JS turned off, selects a file in the widget, and submits the entity form without clicking the Upload button, then the file field is set to be displayed contrary to the field settings and without giving the user the option to display or not.
Comment #15
kim.pepperClosing as outdated. Please re-open if this is still relevant.
Comment #16
catchThis appears to be a real bug, although now instead of always being checked, the 'display' checkbox is always unchecked when you upload a new file regardless of the setting.
The issue is that when you upload a file the checkbox isn't shown at all, so it's empty, but instead of being empty it should be the default value.
Attaching a patch that fixes it.
Comment #18
Arti Anil Pattewar commented#16 Patch has been tested locally in 9.4 Version and the outcome meets as per the requirement.
Comment #19
abhijith s commentedApplied patch #16 on 9.4.x and it works fine.
Before patch:

After patch;

Comment #20
arun velusamy commentedI have verified the patch #16 and tested it against the Drupal version 9.4.x-dev. The patch works fine and I have added the before and after screenshots for reference.
Comment #21
jaykumar95Tested on Drupal 9.4.5
patch applied successfully.
Comment #22
bnjmnm@Arun Velusamy #20 no reason to provide screenshots when they've been provided in an earlier comment. That is not something that will receive issue credit.
@Jaykumar95 #21 providing a screenshot of a patch applying provides no benefit. We have automated tests such as the ones that run in #16 that will fail if a patch can't be applied. This type of comment also receives no credit.
This should not be set to RTBC. Comment #16 correctly said this needs tests, so automated tests should be added to confirm this is addressed.
Comment #24
mohit_aghera commented@catch
This issue seems to be related to claro theme.
I tried to reproduce the bug on simplytest.me for seven and stark theme.
It seems to be working fine for seven and stark theme.
When I switched the theme to claro theme, it is causing issues.
To reproduce the issue, I am uploading two test-only patches.
On local, test only patch with stark
2272637-24-test-only-stark.patchpasses the test cases and same test case is failing for `claro` defaultTheme2272637-24-test-only-claro.patch.Not sure what's wrong with claro theme.
@bnjmnm
Would it be possible for you to double-check the behavior in seven and stark themes?
Comment #26
prasanth_kp commented#24 Patch Applied on 9.5.x and it works fine.
Comment #27
Bushra Shaikh commentedI verified the patch #24 on the drupal 9.5.x version. The patch applied successfully and worked fine.
Testing Results: The state of the checkbox in the DISPLAY column: is checked.
Can be moved to RTBC
RTBC+1
Comment #28
ameymudras commentedThanks for the patch, I tested #24 on 9.5.x and the following are my findings:
1. The issue summary is clear and steps to reproduce have been given
2. Was able to reproduce the issue on 9.5.x
3. The patch provided applies cleanly and seems to fix the issue. The file visibility setting seems to be consistent
4. Tests have been included and test only patch is failing
5. No issues identified with code review
Marking the issue as RTBC, not including additional screenshots, they have been provided in #26
Comment #30
xjmThanks for working on this issue.
The feedback in #24 is valid and has not been addressed. If this only fails in Claro (as per the patches provided), the test needs to use Claro, or it isn't testing anything.
I also think asking for a frontend framework manager to weigh in on why this is different between themes -- is this a bug in Claro? Would a different fix be better? Etc.
Comment #31
xjmComment #32
larowlanI looked at Claro and found it has a file-widget-multiple.html.twig but that doesn't look to be doing anything spurious.
However there's a lot going on in claro_preprocess_file_widget_multiple, including looping over the table rows/cells by reference.
I wonder if this bug occurs with that function commented out. If it doesn't then I think the fix should be there rather than in the FileWidget.
mohit++ for the two tests showing the bug exists in claro but not in stark.
Comment #33
bnjmnmConfirmed this is specific to Claro, and I'm 95% sure it is happening in
_claro_preprocess_file_and_image_widget. It restructures the render array and I'm not sure it's accounting for this value at all, which I believe is in$element["#value"]["display"]Comment #34
mohit_aghera commentedBased on feedback from #32 and #33 removing the change from file module and moving fix the claro theme.
For now, I have added test case into file module itself.
Comment #35
mohit_aghera commentedOops, forgot to upload interdiff.
Re-uploading patch along with interdiff.
Hiding the patch in #34 and #24 as both of them are not relevant.
Comment #37
mohit_aghera commentedAttempting the test case failures and doing code change accordingly.
Both the failing tests are passing on local now.
Comment #38
larowlanLooking good, we can remove the frontend framework manager review tag now we've isolated this to Claro.
Just some minor nits on the test
nit `Claro as the` (capital C and added
as the), probably can remove the word 'few' tooNot needed anymore?
Let's use randomMachineName instead, string includes things like &, > and < and can therefore cause some random failures with CSS selectors (not relevant now, but could become later)
Can we use $this->assertSession->fieldExists() instead
Would be good to also assert the data is stored correctly in the field on the
$node, in addition to asserting it shows correctly in the formMissing a word here, perhaps on 'the' form?
I don't think you need to call this again as its the same node?
Comment #39
pooja saraah commentedAddressed the comment #38-point 1 and 6
Keeping it in Needs Work to address the point 2,3,4,5 and 7
Attached patch and interdiff for #38
Comment #40
ankithashettyAddressed the suggestions made in #38.
1. Comment updated.
2. Removed the
setUp()3. Changed to
randomMachineName().4. Replaced with
$this->assertSession->fieldExists().5. Added assert to
$node->get($field_name)->value.6. Comment updated.
7. No code change was done to address this point in the patch. I believe we need to call
$node = $this->drupalGetNodeByTitle($title);again after every form submit, to get the latest data stored in the DB for the node.Please review.
Thanks!
Comment #42
ankithashettyFixed the phpunit test errors in #40, thanks!
Comment #44
connbi commented#16 the patch is work for me, and the patch which modify the claro.theme is wrong, if user unchecked the Include file in display, the checkbox will always checked.
and the #16 patch is also work on drupal 10, I think the root cause is display not set default value, not in claro theme.
Comment #45
mohit_aghera commented- Refactoring the implementation little bit so we can easily extend for other themes if required.
- Now we are calculating the values in field widget and setting in claro theme.
Patch in #43 had a few failures.
Addressed those as well.
$node = $this->drupalGetNodeByTitle($title);We need to call it with each submit with second argument as `TRUE`, so it pulls latest values from the database rather than cache.
Tests are passing on local now.
Comment #47
mohit_aghera commentedFixing the test case failures.
Tests are passing on local now.
Comment #48
smustgrave commentedCould the issue summary be updated to include proposed solution too.
Comment #49
smustgrave commentedPer #48
Comment #50
mohit_aghera commentedComment #51
mohit_aghera commentedComment #52
smustgrave commentedVerified the issue following the steps in the issue summary.
Applied patch #47
Verified that the Include file in display is now defaulted to check.
Comment #53
xjmThanks everyone for working on this. It's close!
@smustgrave, to get credit for manual testing, you should post before and after screenshots as described in the contributor task doc. Once the patch is updated with my feedback, that will be a good time for someone to provide screenshots, since there have been eight different patch versions and nine months since the most recent screenshots.
I think we need to expand the inline documentation here a little, since the previous comment clearly wasn't enough to write the right code. One inline comment for the if and one for the else, I expect.
This is really nitpicky, but we usually shouldn't refer to "we" (or "you"). It's better to make parts of Drupal the subjects of sentences, or simple passive voice as a last resort.
Here, we can say:
Also, we should add an
@seeto that underscore-namespaced preprocess further down in the docblock."Followed" confused me here. "Respected" maybe?
Also, it's missing the word "the". I understand we're pushing 80 chars, but we can rearrange the sentence a little:
Not a hard requirement, just a suggestion: A couple inline comments for each "paragraph" of code here would make it easier for folks to parse the code in their minds.
Can we use a meaningful test title in each case rather than
randomMachineName()? If we want to test specific characters, we should add tests for those characters. In this case, however, we just want a title, and it can describe the purpose of the item under test. (@larowlan is correct thatrandomString()here would be very bad, but a string literal is better and more readable thanrandomMachineName()in this context as well.)Very-ultra-nit: There should be a comma after "Now".
Nit: "...after the file is uploaded."
Comment #54
ashley_herodev commentedI've just start looking into it, I should be able to provide some feedback to comment #53 soon
Comment #55
ashley_herodev commentedAdded screenshots before and after and addressed items on #53 comment
Comment #56
gauravvvv commentedAddressed feedback from #53, attached patch with interdiff. please review
Comment #57
gauravvvv commentedFixed CC failure issue
Comment #58
ashley_herodev commentedThanks @Gauravvvv for applying the interdiff file 47-55 and recreate the patch for me
Comment #59
smustgrave commented@Gauravvvv this was tagged for novice/new users. Based on your git posts that would not include you :). Please leave novice issues alone for new users.
But what done is done.
Took the screenshots from #55 and added to the IS.
Comment #60
xjm#53.1 was not addressed, oops!
This is 81 characters. :) Drupal has a character limit of 80 characters on code comments, so this comment now needs to be rewrapped.
Drupal inline comments have to be complete sentences (with periods at the end). So for the first comment, I would change it to describe what's important about the data. Something like:
The second can be:
Comment #61
ashley_herodev commentedAdressed comment #60
Comment #62
ashley_herodev commentedFixing the patch in a few moments
Comment #63
ashley_herodev commentedThe patch failed to apply due to trailing whitespace, but since I didn't find any whitespace locally I suspected of an EOL issue. So I just converted
CR LFtoLFComment #64
smustgrave commentedLooking much better, good catch with the line endings
Comment #65
agoradesign commentedworks for me (Drupal 10.1.5 + Gin Admin Theme)
Comment #66
xjmSaving issue credits. @smustgrave is credited for mentoring (the small issue management tasks themselves weren't quite to creditable, but the mentoring is). @bnjmnm, @larowlan, and myself are credited for code reviews etc.
@ameymudras' earlier RTBC comment is kind of borderline -- I'm going to credit it here since it helped sum up some things that were lost in the noise back in January, but in the future I'd recommend doing your own code review and documenting more specific thoughts or steps you had during the code review. (Also, mentioning that you tested with the steps in the IS and exactly what changed from before to after, in words, is helpful even if the screenshots are already provided -- since no one had actually really said that yet when uploading all the duplicate screenshots.)
@pooja saraah, it looks like you've been able to contribute to numerous core issues already -- nice work! Going forward, it would be good to see you addressing the full review (rather than just the easiest points from it) in order to receive issue credit.
@Gauravvvv, you mostly duplicated @ashley_herodev's work in #55. Normally, I would not add credit for this; however, since @ashley_herodev was having some issues with patch encoding, I'll credit it this once. In the future, duplicating an existing patch or small changes like fixing a reroll will not receive credit since you've already made extensive contributions and aren't really a novice anymore. :)
@Arun Velusamy, @Jaykumar95, @Arti Anil Pattewar, @prasanth_kp, and @Bushra Shaikh do not receive credit since they either duplicated previous manual testing or did not provide sufficient explanation and illustration of their manual testing.
Comment #70
xjmCommitted to 11.x, 10.2.x, and 10.1.x as a patch-safe, non-disruptive bugfix. Thanks everyone!
Fixed on commit: some super-small nitpicks that I missed in my previous reviews.
Comment #71
xjmComment #72
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #73
nod_