I had this working in D7, but can't seem to solve the problem in D8. I have a block view on a content type, with a contextual filter of the Content ID. What I want is for the block to not appear if the view is an empty result or there is no result.
I've played with all the settings I could think of: setting the relationship to required. Trying Global:null as an extra filter, both before and after the content filter. I've tried playing with the settings in the Content ID filter. I've changed the setting in all the fields to not show content if no result.
I'm stumped.
Edit: In fact, I'm not sure the system believes there's an empty result. I changed one of the fields to show specific text if no result... it still did nothing.
Comments
+1 having the exact same
+1 having the exact same issue. This issue has been a thorn in Views side for many year. It's a shame there is still no solid UI solution.
Certified Drupal Site Builder 7 & 8
complex problem
This is kind of a complex problem, and one that must be carefully compared with the design expectation, and demonstrated by numerous discussions of this topic in related code contexts:
https://www.drupal.org/node/2443457
https://www.drupal.org/node/2685157
https://www.drupal.org/node/953034
https://www.google.com/search?num=100&q=views+block+hide+empty+"drupal+8"+site%3Adrupal.org
spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...
My bad, I didn't go digging
My bad, I didn't go digging deep enough.
With Drupal 8.3.4 a View 'block display' provides a 'Hide block if the view output is empty' option under Advanced / Other.
Certified Drupal Site Builder 7 & 8
thanks too ...
I hadn't noticed that feature in D8 Views before.
It is nice to see that this option is now an official Views feature.
There it is right in the lower right hand corner of the advanced column of Views configuration.
spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...
Aha! how did I miss that??
Aha! how did I miss that?? Did they just add this?
Thank you!
'Hide block if the view
Thank you! That helped.
One caveat, though
If you have set NO RESULTS BEHAVIOR, the block will (of course) still show, even if you set 'Hide block if the view output is empty' option under Advanced / Other to Yes. And don't forget to clear the views cache before checking results of the change :) (D 8.9.0)
Norbert
-- form follows function
I'm using 8.3.2 version and I
I'm using 8.3.2 version and I have checked the "Hide Block if the view block is empty " as "Yes". Though yes! , Its not hiding the block section.
I am using Drupal 8.3.5 and I
I am using Drupal 8.3.5 and I have the same problem.
Same on Drupal 8.3.6 for me.
Same on Drupal 8.3.6 for me.
Same here... I have set the
Same here... I have set the view to use the current node id, added validation set to hide view if empty, set the field to be hidden if empty, and ticked the code block if empty too and there is a blank sidebar instead of no sidebar...
I'm a Drupal Website Developer and Google Street View Trusted Photographer.
Having the same issues in
Having the same issues in Drupal 8 and I've done the same steps as vaccinemedia. Drupal 8.4.0
I'm experiencing the same
I'm experiencing the same issue on 8.4.4 and I opened an issue already here https://www.drupal.org/project/drupal/issues/2937582.
[Solved] Hide empty block
Article Content Type with two terms: News and Events
Desired: More News Block that appears on each news node and More Events Block that appears on each Event node
Problem: A blank More Events block appears on a News Node along with the More News Block and a blank More News block appears on Event nodes along with More Events block
[Solved]
I have the following Contextual filters
Content: Has taxonomy term ID
Content: ID
Global: Null -> Display all results for the specified field
and
Hide block if the view output is empty: Yes
And the empty block for different term pages does not appear
Also clear 'title' in views UI
Very similar to my use case:
Block configuration in views UI
Advanced:
- contextual filter: content ID
- relationship with core media
- Hide block if the view output is empty = Yes
Filter criteria:
- field IS NOT NULL
The (empty) block would still be visible until I cleared the Title of the view
- Title: none
You'll still be able to add and use a block title from the Block Layout: Block configuration screen
I had an issue similar, I solved it with 2 steps
I had an issue similar, I solved it with 2 steps:
- Use block layout instead of directly printing the view in the template, and I created a little region for that. Also I used this code to make the new region usable directly in the node template: https://atendesigngroup.com/blog/making-region-content-available-node-te...
- Reversing the relationship I was using in the view (that's in my case, but if it can help..). I had on one hand a relationship to display some fields of another content type depending on the reference to the current one, and on the other hand a contextual filter related to the current content type. I reversed the relationship, by deleting it in the fields and fitlering (normal filter) directly to the content type where the fields were, and reversing the relationship, to then put it to the contextual filter.
Also "hide the view if no result" in the advanced parameters and "hide the field if empty" per field.
I need to print views block in template for specific reason
So display "Views Block" from Block region is not a solution in my case.
I need to print views block in template for specific reason.
Any solution?
This is STILL an issue!!
On Drupal 8.7.5 this is still an issue.
I've tried:
- Ticking 'Hide block if the view output is empty'
- 'Hide if empty' in each field's 'No Results Behaviour'
- Adding a filter for my required fields, using 'Not empty (Not NULL)' as the option.
Can anybody please help?
Same issue
+1. ???
And why is this marked as SOLVED? Did I miss something?
Drupal core 8.8.1
I've tried everything suggested here in this thread, still not working, was a solution found ?
Need solution
Hello,
This solution will work if Global NULL / Custom text field is empty. But if I want to hide block if fields are empty and custom text have some value.
Thanks
You must select 'Block' view
You must select 'Block' view (and not Master) in order to find 'Hide block if the view output is empty' under Advanced.
-- Erik
Right!
Yes I found it when I changed the Display to "Block". Thank you Sutharsan.
Default content
The issue on my site was due to the field on a specific node having actual content (a paragraph with empty strings).
Is this really solved?
I am having the same problem and I have run out of alternatives to solve it. I have checked for all present fields, including hidden fields, the option "do not render if empty" and "do not rewrite if empty". I have added Global variable with "Display all fields". I have of course checked "Hide block if empty". I am really running out of alternatives. I am thinking of hiding the block via JS.
I also had this issue today,
I also had this issue today, I had only one field, I tried adding not NULL to that field in the filter criteria and it worked, the view is now hidden.
That solved it for me. Thank
That solved it for me. Thank you.
This worked for me also.
This worked for me also. Thank You!
Thanks, this also solved my
Thanks, this also solved my issue!
Oh finally an actual answer.
Oh finally an actual answer. THANK YOU
Thanks that worked for me!!
Thanks that worked for me!!
Thanks. This is a good work
Thanks. This is a good work around for now.
Thanks
Thnaks so much.
Smart Workaround....!
It Worked.
Thanks..!
we should now find the actual reason behind.. now..!
You the man ! (so damn simple
You the man ! (so damn simple...)
Thank you!
The "Not Null" field filter
The "Not Null" field filter worked for me in Drupal 9. Thank you!
Worked for me too
Worked for me too. Thanks for the workaround. Hopefully they can get this bug fixed soon.
The problem is delayed Block rendering
The underlying issue here has been filed in no less than 4 places in the issue queue, maybe more:
#953034: [meta] Themes improperly check renderable arrays when determining visibility
#2685157: Empty region rendered when a Views block with no results and 'block_hide_empty' set is in the region
#2443457: Views block displayed when no results returned
#2937582: Empty view causes region to be displayed
So this is hopefully a definitive follow-up that if you're still experiencing this issue, the current workaround until a solution to #953034 lands is to first render the output of the region the block is placed in to determine whether it is empty or not by stripping out the rendered tags.
Eg. in page.html.twig in your theme:
Once this gets fixed, then in theory the option inside each Views' Advanced settings panel for Block displays to "Hide block if the view output is empty", should just work.
Other blocks in the region
Doesn't seem to be working for me, though I may be doing it incorrectly for the theme I'm using.
Question: if I understand the logic of the approach above, it assumes the region has no other blocks in it? My situation is that I have a sidebar_second with multiple Views blocks, some of which are sometimes empty, and should disappear, but they're displaying anyway (Views hide if empty is set). Am I correct that this would require using some version of the solution above in a block level template?
Edit: Answer is yes.
Got this working in an Adaptive Theme2 subtheme by altering block.html.twig
If there's a better way to code this, I'd appreciate the feedback.
Relevant section of block.html.twig (I'm using 'blockhasdata' instead of 'has_sidebar_second')
I have a solution that should work in most cases
I found Drupal 8+ to be very finicky when it comes to triggering "No Results Behavior" (which is a great test to see if views thinks the display is indeed empty; do not leave this set though (see below)). It seems if you're showing fields of certain types (e.g. body), even if you have all of the following set, it still thinks the display is NOT empty:
Indeed, if you look at the output from an empty view display, you will usually get at least one empty row. I can't say why views doesn't consider a display with all empty rows as "No Results", but that seems to be the case. (To me, this is a bug.) I even went so far to as to check the database for the fields in question, and there was no entry for the NID in question, so I can't fathom why views creates a row to begin with.
However, all that being said, there's a relatively simple solution: add a field to the filter criteria that must be present for your view to work, and set the operator to "Is not empty". If you get duplicate rows because of this, there is a patch here: https://www.drupal.org/project/drupal/issues/2998016
The patch is used in conjunction with the "Distinct" Query option.
Once you have the filter in place, you should start seeing No Results Behavior triggered when it should be, and then you should be able to hide the view as needed. Side note: a view will not be considered empty if you have No Results Behavior set.
Looking to Migrate to Drupal 9/10? Have a look at our Drupal 9 demo site and request access.
Genius fix, worked for me.
Genius fix, worked for me. Thanks!