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
- Create some dummy nodes with an empty body
- Create a REST view
- Showing fields add body
- In the field settings check "hide if empty"
- 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.
| Comment | File | Size | Author |
|---|---|---|---|
| #51 | 2884879-51-remove-empty-setting.patch | 730 bytes | smustgrave |
| #46 | 2884879-46.patch | 12.86 KB | smustgrave |
| #46 | interdiff-41-46.txt | 1.49 KB | smustgrave |
Comments
Comment #2
saranya ashokkumar commentedHi Anishnirmal,
I also faced the same issue and I have created patch for that.
Thanks,
Saranya.P
Comment #3
saranya ashokkumar commentedComment #4
dinesh18 commentedManually reviewed the patch and it doesn't seems to follow the Drupal Coding standards
Could you please have a look at it.
Comment #5
lendudeSee 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.
Comment #6
wim leersComment #7
prajaankit commentedHi @Dinesh18
Thanks for find coding standards issue, i remove all these errors.
Comment #8
prajaankit commentedComment #9
prajaankit commentedRename patch please review
Comment #10
wim leersThanks! However, to prove that this is indeed solving the problem, this needs tests.
Comment #11
jofitzThe 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?
Comment #12
prajaankit commentedHi Jo Fitzgerald,
Thanks for review
I changed as per #4 comments
Well done you do the great job. :)
Comment #13
wim leers\Drupal\comment\Tests\Views\CommentRestExportTestis 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 :(Comment #14
wim leersfor tests.
Comment #16
kil commentedRunning 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?Comment #19
milovan commented#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).
Comment #22
Apnc commentedWe 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.
Comment #23
Apnc commentedChecking 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.
Comment #24
powysm commentedI had problems with #11 and also #23 after upgrading to Drupal 9 from Drupal 8.
Have attached the version thats currently working for me.
Comment #25
edilsonhc commentedWorking 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_filterif the raw output is an array.Comment #26
edilsonhc commentedWorking 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.
Comment #27
edilsonhc commentedWorking 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.
Comment #28
edilsonhc commentedComment #31
edilsonhc commentedComment #33
eleonelAttached patch using https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%... to check if the value is empy:
Adding:
right before the check for
// Omit excluded fields from the rendered output.Comment #35
eleonelComment #36
eleonelAttached patch with a test included:
To run it:
Comment #37
naveenvalechaLooks good to me.
Comment #38
naveenvalechaThere's no need to explicitly install the standard profile. The testing profile should be sufficient for it.
Comment #39
eleonelThanks Naveen, I'm using the
standardprofile because I need to have abodyfield in the article content type, also we may need extra tests to check:field_imageandfield_tagsfrom the same content type and all those fields are provided by default onstandardprofile.Comment #40
smustgrave commentedTesting #36 and it works. Attaching screenshots.
For the committer can we have tests-only patch also to make sure it's working.
Comment #41
smustgrave commentedPatch is the same as #36 but with a tests-only patch also.
Comment #43
smustgrave commentedsince I didn't make any change just was checking the testing I think I can mark this as RTBC
Comment #44
quietone commentedThe 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.
I don't recall seeing @see lines in a test before. Is the intention to be an @covers or something else?
Missing one line description.
This comment is not adding any useful information. Let's remove it.
Should this not be in the views component?
Comment #45
lendudeAlso, 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.
Comment #46
smustgrave commentedUpdated IS summary.
Also addressed #44 1-3. and #45
Could see it there or here. Not sure.
Comment #47
naveenvalechaThanks! Looks good to me.
I see the concerns in #38, #44, and #45 has been addressed
Comment #48
naveenvalechaComment #49
alexpottI 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.
Comment #50
borisson_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.
Comment #51
smustgrave commentedSo like this?
Moving to 9.5.x
Used a special file naming because this patch is following a different approach
Comment #52
bramdriesenChange 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.
Comment #53
lendudeNo 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).
Comment #55
iamfredrik commentedYou 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.
Comment #56
jwilson3I 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.
Comment #57
jwilson3Comment #59
kobusvw commentedI wanted to check in regarding the progress of this ticket, which was last updated on 15 March 2023. Could you kindly provide an update on its status? Is there any consideration for a potential fix in the near future?
Comment #60
borisson_The latest version of the patch is to disable the option in the views UI, but the issue summary doesn't yet reflect that. So that would be the first thing to do here. As a next step we should transform the patch into a merge request.
Comment #61
cb_govcms commented+1 to change in approach. This is extended by the CSV serialisation module and skipping empty fields would be incorrect behaviour in that scenario.
However, I think the latest patch does too much in removing all the 'No results behaviour' options. I think supplying 'No results text' etc are still valid options in this context for a user to make so only the single `hide_empty` checkbox should be removed.