Issue summary updated as of comment #44

Problem/Motivation

When creating a REST view with fields. When checking "hide if empty" the field is still appearing in the feed.

Steps to reproduce

  1. Create some dummy nodes with an empty body
  2. Create a REST view
  3. Showing fields add body
  4. In the field settings check "hide if empty"
  5. In the preview see that the body field is still there.

Proposed resolution

If the field is empty and "hide if empty" is checked I would expect the field to not appear in the feed.

Remaining tasks

Code review

User interface changes

API changes

This could affect users who are having to put checks into their feeds for empty values - Maybe

Data model changes

Release notes snippet

The option in REST views to "Hide If Empty" was not being respected and the field items were always shown in the rest view regardless of the setting. To keep backwards compatibility it was chosen to simply remove the "Hide If Empty" option. This removal has no effect on existing sites that might have set the option since it was disregarded.

Original report by Anishnirmal

Hi,

I have created view with rest export and I configured field for that view. For Fields under "No Result Behavior", I have enabled "hide if empty" option to hide the field which has no value. But in view result, that field didn't get hide.

CommentFileSizeAuthor
#51 2884879-51-remove-empty-setting.patch730 bytessmustgrave
#46 2884879-46.patch12.86 KBsmustgrave
#46 interdiff-41-46.txt1.49 KBsmustgrave
#41 2884879-41.patch13.07 KBsmustgrave
#41 2884879-41-tests-only.patch12.31 KBsmustgrave
#40 after-patch.png41.37 KBsmustgrave
#40 before-patch.png79.14 KBsmustgrave
#36 2884879_36.patch13.07 KBeleonel
#36 33-36-interdiff.txt13.21 KBeleonel
#33 27-33-interdiff.txt946 byteseleonel
#33 2884879_33.patch769 byteseleonel
#27 2884879_27.patch810 bytesedilsonhc
#26 2884879_26.patch822 bytesedilsonhc
#25 2884879_25.patch822 bytesedilsonhc
#24 2884879-d9.patch681 bytespowysm
#23 2884879-23.patch798 bytesApnc
#22 interdiff_11-22.txt612 bytesApnc
#22 2884879-22.patch677 bytesApnc
#11 2884879-11.patch655 bytesjofitz
#11 interdiff-9-11.txt873 bytesjofitz
#9 views_core_hide_if_empty_2884879-3.patch802 bytesprajaankit
#7 views_core_hide_if_empty_2884879-14.patch802 bytesprajaankit
#2 views_core_hide_if_empty_2884879.patch872 bytessaranya ashokkumar
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anishnirmal created an issue. See original summary.

saranya ashokkumar’s picture

Hi Anishnirmal,

I also faced the same issue and I have created patch for that.

Thanks,
Saranya.P

saranya ashokkumar’s picture

Status: Active » Needs review
Dinesh18’s picture

Status: Needs review » Needs work
       // Omit excluded fields from the rendered output.
-      if (empty($field->options['exclude'])) {
-        $output[$this->getFieldKeyAlias($id)] = $value;
+       if (empty($field->options['exclude'])) {
+        if(!empty($field->options['hide_empty'])) { // hide if empty
+          if(!empty($value) ) {
+            $output[$this->getFieldKeyAlias($id)] = $value;
+          }
+        }
+        else { // show all fields
+          $output[$this->getFieldKeyAlias($id)] = $value;
+        }
       }
     }

Manually reviewed the patch and it doesn't seems to follow the Drupal Coding standards
Could you please have a look at it.

Lendude’s picture

Component: views.module » rest.module
Issue tags: +VDC
Related issues: +#2606548: \Drupal\rest\Plugin\views\row\DataFieldRow::render should take into account the 'exclude' flag

See the discussion on the existing code here: #2606548: \Drupal\rest\Plugin\views\row\DataFieldRow::render should take into account the 'exclude' flag. I'm not getting into that discussion again, especially since the security implications aren't there for this instance.

Hiding a field when empty sounds like really weird behaviour in a REST service to me, but if we offer the option, we should respect it. So in that sense we should fix it, but in my opinion it would be a better fix to remove the option to do so.

Wim Leers’s picture

Title: "Hide If Empty" under No Result Behavior in views rest export (Field Level) » REST views: "Hide If Empty" under No Result (Field Level)
Issue tags: +API-First Initiative, +Needs tests
prajaankit’s picture

Hi @Dinesh18

Thanks for find coding standards issue, i remove all these errors.

prajaankit’s picture

Status: Needs work » Needs review
prajaankit’s picture

Rename patch please review

Wim Leers’s picture

Status: Needs review » Needs work

Thanks! However, to prove that this is indeed solving the problem, this needs tests.

jofitz’s picture

Status: Needs work » Needs review
FileSize
873 bytes
655 bytes

The patch in #9 appears to be the same as #7, regardless it still doesn't meet with coding standards. I have also simplified the logic.

@Wim Leers Is there already a test for this class that we can add to? Or does this require its own new test file?

prajaankit’s picture

Hi Jo Fitzgerald,
Thanks for review
I changed as per #4 comments
Well done you do the great job. :)

Wim Leers’s picture

\Drupal\comment\Tests\Views\CommentRestExportTest is the only functional Views REST export test that I can find. So that's the only place you might be able to add test coverage to. Yes, REST + Views has barely any test coverage :(

Wim Leers’s picture

Status: Needs review » Needs work

Needs work for tests.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

kil’s picture

Running into the same problem, that the view option "Hide if empty" is ignored. When applying the patch from #11, we discovered, that a simple !empty($value) will only work with raw output but not with rendered values, e.g. images (the direct lines before the patch allow either a $field->getValue() or a $field->advancedRender()). One solution could be to rely always on the raw output, therefore instead of !empty($value) asking for !empty($field->getValue($row)). I'm not sure, if this is a nice solution or a bad workaround. Any opinons on this?

       // Omit excluded fields from the rendered output.
       if (empty($field->options['exclude'])) {
-        $output[$this->getFieldKeyAlias($id)] = $value;
+        if (empty($field->options['hide_empty']) || !empty($field->getValue($row))) {
+          $output[$this->getFieldKeyAlias($id)] = $value;
+        }
       }
     }

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

milovan’s picture

#16 That change indeed helped hiding image link when there is no image, without using raw value (which always prints nid where image is attached).

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Apnc’s picture

We had the same problem as #16 points out. I've updated the patch from #11 with their suggestion. It does make sense to me to check the raw value instead of a rendered one.

Apnc’s picture

Checking for empty() alone makes it so integer 0 or string "0" are hidden too which should not be the default case. This version of the patch checks for NULL or empty string instead.

powysm’s picture

I had problems with #11 and also #23 after upgrading to Drupal 9 from Drupal 8.
Have attached the version thats currently working for me.

edilsonhc’s picture

FileSize
822 bytes

Working with the custom preprocessor the raw integer value could have the following content:
[0 => 0], this should be empty.
Check if !empty() is not enough.

I have added a validation using array_filter if the raw output is an array.

edilsonhc’s picture

Working with the custom preprocessor the raw integer value could have the following content:
[0 => 0], this should be empty.
Check if !empty() is not enough.

I have added a validation using array_filter if the raw output is an array.

#25 has a typo, use this instead.

edilsonhc’s picture

Working with the custom preprocessor the raw integer value could have the following content:
[0 => 0], this should be empty.
Check if !empty() is not enough.

I have added a validation using array_filter if the raw output is an array.

#26 has a typo (again), use this instead.

edilsonhc’s picture

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
edilsonhc’s picture

Status: Needs work » Needs review

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

eleonel’s picture

Attached patch using https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%... to check if the value is empy:

Adding:

// Omit 'Hide if empty' fields from the rendered output for empty values.
      if ($field->options['hide_empty'] && $field->isValueEmpty($value, $field->options['empty_zero'])) {
        continue;
      }

right before the check for // Omit excluded fields from the rendered output.

Status: Needs review » Needs work

The last submitted patch, 33: 2884879_33.patch, failed testing. View results

eleonel’s picture

Status: Needs work » Needs review
eleonel’s picture

Attached patch with a test included:

/**
   * Tests the display of an "hide if empty" body field.
   */
  public function testHideIfEmptyBodyDisplay() {
    $actual_json = $this->drupalGet($this->view->getPath(), ['query' => ['_format' => 'json']]);
    $this->assertSession()->statusCodeEquals(200);
    $expected = [
      ['title' => 'Article Title 10', 'body' => "<p>Article test 10</p>\n"],
      ['title' => 'Article Title 9'],
      ['title' => 'Article Title 8', 'body' => "<p>Article test 8</p>\n"],
      ['title' => 'Article Title 7'],
      ['title' => 'Article Title 6', 'body' => "<p>Article test 6</p>\n"],
      ['title' => 'Article Title 5'],
      ['title' => 'Article Title 4', 'body' => "<p>Article test 4</p>\n"],
      ['title' => 'Article Title 3'],
      ['title' => 'Article Title 2', 'body' => "<p>Article test 2</p>\n"],
      ['title' => 'Article Title 1'],
    ];

    $expected_json = json_encode($expected, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
    $this->assertSame($expected_json, $actual_json);
  }

To run it:

$ cd core
$ ../vendor/bin/phpunit --filter testHideIfEmptyBodyDisplay modules/rest/tests/src/Functional/Views/HideIfEmptyFieldTest.php
naveenvalecha’s picture

Issue tags: -Needs tests

Looks good to me.

naveenvalecha’s picture

+++ b/core/modules/rest/tests/src/Functional/Views/HideIfEmptyFieldTest.php
@@ -0,0 +1,108 @@
+  protected $profile = 'standard';

There's no need to explicitly install the standard profile. The testing profile should be sufficient for it.

eleonel’s picture

Thanks Naveen, I'm using the standard profile because I need to have a body field in the article content type, also we may need extra tests to check: field_image and field_tags from the same content type and all those fields are provided by default on standard profile.

smustgrave’s picture

FileSize
79.14 KB
41.37 KB

Testing #36 and it works. Attaching screenshots.

For the committer can we have tests-only patch also to make sure it's working.

smustgrave’s picture

Patch is the same as #36 but with a tests-only patch also.

The last submitted patch, 41: 2884879-41-tests-only.patch, failed testing. View results

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

since I didn't make any change just was checking the testing I think I can mark this as RTBC

quietone’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

The Issue Summary here is incomplete. Write an issue summary for an existing issue has guidance. The changes should include the comment in #5 because it questions if this should be done at all. Adding tag.

in #37, it is said that 'this looks good' There is no indication if that means a patch review was done or simply that the patch works as expected. This needs a code review as well.

My comments below do not constitute a full code review.

  1. +++ b/core/modules/rest/tests/src/Functional/Views/HideIfEmptyFieldTest.php
    @@ -0,0 +1,108 @@
    + * @see \Drupal\rest\Plugin\views\display\RestExport
    + * @see \Drupal\rest\Plugin\views\row\DataFieldRow
    

    I don't recall seeing @see lines in a test before. Is the intention to be an @covers or something else?

  2. +++ b/core/modules/rest/tests/src/Functional/Views/HideIfEmptyFieldTest.php
    @@ -0,0 +1,108 @@
    +   * @var \Drupal\views\ViewExecutable
    

    Missing one line description.

  3. +++ b/core/modules/rest/tests/src/Functional/Views/HideIfEmptyFieldTest.php
    @@ -0,0 +1,108 @@
    +   * The profile to install as a basis for testing.
    

    This comment is not adding any useful information. Let's remove it.

Should this not be in the views component?

Lendude’s picture

+++ b/core/modules/rest/tests/src/Functional/Views/HideIfEmptyFieldTest.php
@@ -0,0 +1,108 @@
+  protected $profile = 'standard';

Also, the standard profile having some fields you need isn't a good enough reason to use that, the overhead of using standard vs testing profile is massive, please just add the fields and content types you need in the test, since this also makes it much clearer what is being tested.

Still not convinced we shouldn't just remove the option, but this might not be that easy for just Rest view fields. So to answer my own feedback in #5: this is probably the best way to do this.

smustgrave’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs issue summary update
FileSize
1.49 KB
12.86 KB

Updated IS summary.

Also addressed #44 1-3. and #45

Should this not be in the views component?

Could see it there or here. Not sure.

naveenvalecha’s picture

Status: Needs review » Reviewed & tested by the community

Thanks! Looks good to me.
I see the concerns in #38, #44, and #45 has been addressed

naveenvalecha’s picture

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

I feel that #5 has never been properly answered. I think we need a use-case here were this makes sense. At the moment we run the risk of breaking people's assumptions because atm empty values are being added to the REST output.

I think if no use-case can be found then I think we should remove the functionality and maintain the behaviour. It's less risky to existing sites.

borisson_’s picture

I agree with @Lendude and @alexpott here, removing the functionality for rest maintains bc and is less risky. It would also be a very weird behavior to only sometimes include the field in the same response.

smustgrave’s picture

Version: 9.4.x-dev » 9.5.x-dev
FileSize
730 bytes

So like this?

Moving to 9.5.x

Used a special file naming because this patch is following a different approach

BramDriesen’s picture

Title: REST views: "Hide If Empty" under No Result (Field Level) » REST views: remove "Hide If Empty" option (Field Level)
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs change record

Change record created https://www.drupal.org/node/3313620
Also added it to the release notes (not sure if this is needed for this issue).

Latest patch still needs review and so does the change record/release note. Re-worded the title a bit.

Lendude’s picture

Status: Needs review » Needs work
+++ b/core/modules/rest/rest.module
@@ -34,3 +34,16 @@ function rest_help($route_name, RouteMatchInterface $route_match) {
+function rest_form_views_ui_config_item_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == "views_ui_config_item_form") {

No need to check the $form_id in a form ID specific hook, it should only get called when the form ID is that form ID.

This seems way too generic though. Won't it remove the option for all Views fields when you enable the Rest module? We need to specifically target the form then it is being used in a Rest display (not a clue how to do that from the top of my head, sorry).

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

iamfredrik’s picture

You may remove the option by default for backwards compatibility, but please make the option to hide fields into a setting. My use case is that I have a bunch of different content types with different fields. I use the grouping patch from here https://www.drupal.org/project/drupal/issues/3251937 to group the fields by content type. In this case it doesn't make sense to show empty fields for those content types that don't have the fields.

jwilson3’s picture

I think this issue still needs an IS update. The title of the issue has been updated to reflect that the approach in the patches is to remove the "Hide if empty" option, however the Proposed resolution is still talking about the expected behavior when the option is checked, which is the inverse of removing the option entirely.

jwilson3’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.