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

Stephen Ollman’s picture

+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

sprite’s picture

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

Stephen Ollman’s picture

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

sprite’s picture

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

turpentyne’s picture

Aha! how did I miss that?? Did they just add this?

Thank you!

AWD Studio’s picture

'Hide block if the view output is empty' option under Advanced / Other.

Thank you! That helped.

nofue’s picture

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

Logeshvaran’s picture

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.

mfgr’s picture

I am using Drupal 8.3.5 and I have the same problem.

mocasalter’s picture

Same on Drupal 8.3.6 for me.

vaccinemedia’s picture

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.

jibrille’s picture

Having the same issues in Drupal 8 and I've done the same steps as vaccinemedia. Drupal 8.4.0

leolandotan’s picture

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.

gmasky’s picture

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

ph7’s picture

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

Tritof’s picture

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.

kalidasan’s picture

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?

WhereverPanda’s picture

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?

mtdaveo’s picture

+1.  ???

And why is this marked as SOLVED?  Did I miss something?

bayorker’s picture

I've tried everything suggested here in this thread, still not working, was a solution found ?

hiralthaker’s picture

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

Sutharsan’s picture

You must select 'Block' view (and not Master) in order to find 'Hide block if the view output is empty' under Advanced.

-- Erik

dimitris3’s picture

Yes I found it when I changed the Display to "Block". Thank you Sutharsan.

ccjjmartin’s picture

The issue on my site was due to the field on a specific node having actual content (a paragraph with empty strings).

larean’s picture

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.

montesajudy’s picture

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.

PhilippJC’s picture

That solved it for me. Thank you.

StefanRoman’s picture

This worked for me also. Thank You!

nickgrace’s picture

Thanks, this also solved my issue!

TheJDexp’s picture

Oh finally an actual answer. THANK YOU

amitajgaonkar’s picture

Thanks that worked for me!!

jgunawan’s picture

Thanks. This is a good work around for now.

alyaj2a’s picture

Thnaks so much.

dandekarprashant’s picture

It Worked.

Thanks..!
we should now find the actual reason behind.. now..!

sneakysnk’s picture

You the man ! (so damn simple...)

Thank you!

hockey2112’s picture

The "Not Null" field filter worked for me in Drupal 9. Thank you!

tonytheferg’s picture

Worked for me too. Thanks for the workaround. Hopefully they can get this bug fixed soon.

jwilson3’s picture

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:

{# render and strip out everything except images, iframes, objects and svgs that don't have any inner html #}
{% set has_sidebar_second = page.sidebar_second|render|striptags('<img><iframe><object><svg>')|trim|length %}

...

{% if has_sidebar_second %}
  {{ page.sidebar_second }}
{% endif %}

...

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.

aharown07’s picture

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')

{% set blockhasdata = content|render|striptags('<img><iframe><object><svg>')|trim|length %}

{%-
  set classes = [
    'block',
    'block-config-provider--' ~ configuration.provider|clean_class,
    'block-plugin-id--' ~ plugin_id_clean|clean_class,
    label ? 'has-title',
    bundle ? 'block--type-' ~ bundle|clean_class,
    view_mode ? 'block--view-mode-' ~ view_mode|clean_class,
  ]
-%}

{% if blockhasdata %}

{%- set heading_id = attributes.id ~ '-title'|clean_id -%}
<div{{ attributes.addClass(classes)|without('role', 'aria-labelledby') }}{%- if label %} role="region" aria-labelled
  <div class="block__inner">

    {{ title_prefix }}
    {%- if label -%}
      <h2 {{ title_attributes.addClass('block__title').setAttribute('id', heading_id) }}><span>{{ label }}</span></h
    {%- endif -%}
    {{ title_suffix }}


    {%- block content -%}
      <div{{ content_attributes.addClass('block__content') }}>
        {{- content -}}
      </div>
    {%- endblock -%}

  </div>
</div>
{% endif %}
webdrips’s picture

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:

  1. Hide empty fields (Rows style options)
  2. Hide if empty (every field No results behavior)
  3. Hide block if the view output is empty (assuming a block display)

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.

hockey2112’s picture

Genius fix, worked for me. Thanks!