Default file visibility setting not respected

Created on 22 May 2014, over 10 years ago
Updated 10 November 2023, about 1 year ago

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 🐛 Default file visibility setting not respected RTBC we noticed that issue seems to be in claro theme.

So overall solution is:

  1. Set the display flag in the field widget so other themes can use it
  2. 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

🐛 Bug report
Status

Fixed

Version

10.1

Component
File system 

Last updated 1 day ago

Created by

🇧🇪Belgium malc0mn

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India pooja saraah Chennai

    Addressed 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

  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India ankithashetty Karnataka, India

    Addressed 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!

  • 🇮🇳India ankithashetty Karnataka, India

    Fixed the phpunit test errors in #40, thanks!

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,469 pass, 2 fail
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,473 pass
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    29,470 pass
  • Status changed to Needs work about 1 year ago
  • #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.

  • Status changed to Needs review about 1 year ago
  • last update about 1 year ago
    30,146 pass, 2 fail
  • 🇮🇳India mohit_aghera Rajkot

    - 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.

  • last update about 1 year ago
    30,168 pass
  • 🇮🇳India mohit_aghera Rajkot

    Fixing the test case failures.
    Tests are passing on local now.

  • 🇺🇸United States smustgrave

    Could the issue summary be updated to include proposed solution too.

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Per #48

  • Status changed to Needs review about 1 year ago
  • Status changed to RTBC about 1 year ago
  • 🇺🇸United States smustgrave

    Verified the issue following the steps in the issue summary.
    Applied patch #47
    Verified that the Include file in display is now defaulted to check.

  • last update about 1 year ago
    30,212 pass
  • last update about 1 year ago
    30,366 pass
  • last update about 1 year ago
    30,364 pass
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States xjm

    Thanks 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.

    1. +++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
      @@ -321,7 +321,12 @@ public static function value($element, $input, FormStateInterface $form_state) {
      +        if (empty($input['fids'])) {
      +          $input['display'] = $element['#display_default'];
      +        }
      +        else {
      +          $input['display'] = $element['#display_field'] ? 0 : 1;
      +        }
      

      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.

    2. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -0,0 +1,100 @@
      + * We are intentionally testing it with Claro as the default theme to test the
      + * changes added in _claro_preprocess_file_and_image_widget().
      

      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:

      The widget is tested with Claro as the default theme to test the [...etc.]

      Also, we should add an @see to that underscore-namespaced preprocess further down in the docblock.

    3. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -0,0 +1,100 @@
      +   * Tests that visibility settings from field widget are followed on the form.
      

      "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:

      Tests that the field widget visibility settings are respected on the form.

    4. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -0,0 +1,100 @@
      +    $type_name = 'article';
      +    $field_name = 'test_file_field_1';
      +    $field_storage_settings = [
      +      'display_field' => TRUE,
      +      'display_default' => TRUE,
      +    ];
      +    $field_settings = [];
      +    $widget_settings = [];
      +    $field_storage = $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
      +
      +    $page = $this->getSession()->getPage();
      +    $assert_session = $this->assertSession();
      +
      +    $test_file = current($this->getTestFiles('text'));
      +    $test_file_path = \Drupal::service('file_system')->realpath($test_file->uri);
      +
      +    $this->drupalGet("node/add/$type_name");
      

      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.

    5. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -0,0 +1,100 @@
      +    $title = $this->randomMachineName();
      ...
      +    $title = $this->randomMachineName();
      

      Can we use a meaningful test title 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. (@larowlan is correct that randomString() here would be very bad, but a string literal is better and more readable than randomMachineName() in this context as well.)

    6. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -0,0 +1,100 @@
      +    // Now submit the same form and ensure that value is retained.
      

      Very-ultra-nit: There should be a comma after "Now".

    7. +++ b/core/themes/claro/claro.theme
      @@ -1382,6 +1382,11 @@ function _claro_preprocess_file_and_image_widget(array &$variables) {
      +  // Handle the default checkbox display after file is uploaded.
      

      Nit: "...after the file is uploaded."

  • 🇧🇷Brazil ashley_herodev

    I've just start looking into it, I should be able to provide some feedback to comment #53 soon

  • Status changed to Needs review about 1 year ago
  • last update about 1 year ago
    Patch Failed to Apply
  • 🇧🇷Brazil ashley_herodev

    Added screenshots before and after and addressed items on #53 comment

  • last update about 1 year ago
    Custom Commands Failed
  • 🇮🇳India gauravvvv Delhi, India

    Addressed feedback from #53, attached patch with interdiff. please review

  • last update about 1 year ago
    30,375 pass
  • 🇮🇳India gauravvvv Delhi, India

    Fixed CC failure issue

  • 🇧🇷Brazil ashley_herodev

    Thanks @Gauravvvv for applying the interdiff file 47-55 and recreate the patch for me

  • Status changed to RTBC about 1 year ago
  • 🇺🇸United States smustgrave

    @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.

  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States xjm
    1. #53.1 was not addressed, oops!
       

    2. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -7,9 +7,11 @@
      - * We are intentionally testing it with Claro as the default theme to test the
      + * The widget is intentionally tested with Claro as the default theme to test the
        * changes added in _claro_preprocess_file_and_image_widget().
      

      This is 81 characters. :) Drupal has a character limit of 80 characters on code comments, so this comment now needs to be rewrapped.

    3. +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
      @@ -20,9 +22,10 @@ class FileFieldWidgetClaroThemeTest extends FileFieldWidgetTest {
      +    // Given the initial data below
      
      @@ -39,8 +42,9 @@ public function testWidgetDefaultVisibilitySettings(): void {
      +    // Filling the form accordingly
      

      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:

      Set up an article node with whatever whatever about the display settings.

      The second can be:

      Fill out the form accordingly.

  • Status changed to Needs review about 1 year ago
  • last update about 1 year ago
    Patch Failed to Apply
  • 🇧🇷Brazil ashley_herodev

    Adressed comment #60

  • 🇧🇷Brazil ashley_herodev

    Fixing the patch in a few moments

  • last update about 1 year ago
    30,383 pass
  • 🇧🇷Brazil ashley_herodev

    The 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 LF to LF

  • Status changed to RTBC about 1 year ago
  • 🇺🇸United States smustgrave

    Looking much better, good catch with the line endings

  • last update about 1 year ago
    30,381 pass
  • last update about 1 year ago
    30,386 pass
  • 47:27
    46:16
    Running
  • last update about 1 year ago
    30,401 pass
  • last update about 1 year ago
    30,401 pass
  • last update about 1 year ago
    30,417 pass
  • 🇦🇹Austria agoradesign

    works for me (Drupal 10.1.5 + Gin Admin Theme)

  • last update about 1 year ago
    30,421 pass
  • last update about 1 year ago
    30,430 pass
  • last update about 1 year ago
    30,430 pass
  • last update about 1 year ago
    30,441 pass
  • last update about 1 year ago
    30,442 pass
  • last update about 1 year ago
    30,468 pass
  • last update about 1 year ago
    30,485 pass
  • last update about 1 year ago
    30,487 pass
  • last update about 1 year ago
    30,490 pass
  • last update about 1 year ago
    30,492 pass
  • last update about 1 year ago
    30,514 pass
  • 🇺🇸United States xjm

    Saving issue credits. @smustgrave is credited for mentoring (the small issue management tasks themselves weren't quite to creditable, but the mentoring is). @bnjmnm and @larowlan 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.

    • xjm committed 41668b6b on 11.x
      Issue #2272637 by mohit_aghera, ashley_herodev, ankithashetty, Gauravvvv...
    • xjm committed 4f214398 on 10.2.x
      Issue #2272637 by mohit_aghera, ashley_herodev, ankithashetty, Gauravvvv...
    • xjm committed 70dcf6dd on 10.1.x
      Issue #2272637 by mohit_aghera, ashley_herodev, ankithashetty, Gauravvvv...
  • 🇺🇸United States xjm

    Committed 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.

    diff --git a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
    index 29abb041432..c97a4d20b38 100644
    --- a/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
    +++ b/core/modules/file/src/Plugin/Field/FieldWidget/FileWidget.php
    @@ -325,7 +325,7 @@ public static function value($element, $input, FormStateInterface $form_state) {
               $input['display'] = $element['#display_default'];
             }
             // Checkboxes lose their value when empty.
    -        // If the display field is present make sure its unchecked value is
    +        // If the display field is present, make sure its unchecked value is
             // saved.
             else {
               $input['display'] = $element['#display_field'] ? 0 : 1;
    diff --git a/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
    index af2cd7ec6ce..69bbaf3bf1e 100644
    --- a/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
    +++ b/core/modules/file/tests/src/FunctionalJavascript/FileFieldWidgetClaroThemeTest.php
    @@ -5,7 +5,7 @@
     use Drupal\Core\Url;
     
     /**
    - * Tests the widget visibility settings for Claro theme.
    + * Tests the widget visibility settings for the Claro theme.
      *
      * The widget is intentionally tested with Claro as the default theme to test
      * the changes added in _claro_preprocess_file_and_image_widget().
    @@ -25,7 +25,7 @@ class FileFieldWidgetClaroThemeTest extends FileFieldWidgetTest {
        * Tests that the field widget visibility settings are respected on the form.
        */
       public function testWidgetDefaultVisibilitySettings(): void {
    -    // Set up an article node with all field storage settings as true.
    +    // Set up an article node with all field storage settings set to TRUE.
         $type_name = 'article';
         $field_name = 'test_file_field_1';
         $field_storage_settings = [
    @@ -69,7 +69,7 @@ public function testWidgetDefaultVisibilitySettings(): void {
         $this->drupalGet(Url::fromRoute('entity.node.edit_form', ['node' => $node->id()]));
         $assert_session->checkboxNotChecked("{$field_name}[0][display]");
     
    -    // Disable the field settings and ensure that settings are updated on form.
    +    // Disable the field settings and ensure that the form is updated.
         $field_storage->setSetting('display_default', FALSE);
         $field_storage->save();
         $page = $this->getSession()->getPage();
    @@ -84,14 +84,14 @@ public function testWidgetDefaultVisibilitySettings(): void {
         $this->assertEquals($type, 'checkbox');
         $assert_session->checkboxNotChecked("{$field_name}[0][display]");
     
    -    // Now submit the same form and ensure that value is retained.
    +    // Submit the same form and ensure that value is retained.
         $this->submitForm([], 'Save');
         $node = $this->drupalGetNodeByTitle($title, TRUE);
         $this->assertEquals(0, $node->get($field_name)->getValue()[0]['display'], 'The checkbox is disabled.');
         $this->drupalGet(Url::fromRoute('entity.node.edit_form', ['node' => $node->id()]));
         $assert_session->checkboxNotChecked("{$field_name}[0][display]");
     
    -    // Now check the checkbox and ensure that it is submitted properly.
    +    // Check the checkbox and ensure that it is submitted properly.
         $this->submitForm([
           "{$field_name}[0][display]" => TRUE,
         ], 'Save');
    
  • Status changed to Fixed about 1 year ago
  • 🇺🇸United States xjm
  • Status changed to Needs work about 1 year ago
  • The 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.)

  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024