Hi, Im using Drupal 7 and I thought it would do this automatically, I have a block with a view in it and it wont hide the block if there is nothing in it, does anyone know what setting I need for this?

thanks

Comments

pixelsweatshop’s picture

1. Go to the advanced tab of you view
2. Add contextual filters
3. Select Global: Null
4. When the filter value is NOT available: Select 'Hide view'

aniebel’s picture

Thank you nicoz, this saved me!

groston’s picture

Make that two likes!

Nick Fedchik’s picture

Thank You for the hint!!!

It hide blocks. But anyway.
In my case I made a blocks to show some fields if a node on a node page.
I have two context filters: Content:NID (Content ID from URL) then add another Global:NULL (Hide content), but blocks became hidden anyway even they have a content (text in my case).

scotwith1t’s picture

Switch the order/weight of the filters (click rearrange) and see if that helps?

halth’s picture

@Nick Fedchik On the "Content ID from URL" contextual filter you should check the option below: "When the filter value IS available or a default is provided -> Specify validation criteria".

On the Validator option, choose PHP Code.
On the PHP validate code put your custom validation code.
E.g.: Check if some mandatory field is empty.

In my case the value of the PHP validate code field was:

$node = node_load($argument);
return (is_array($node->field_fotos) && count($node->field_fotos) > 0);

And then, in the Action to take if filter value does not validate option, choose: Hide View.

Hope this works for all you guys! =)

--
Heitor Althmann
Drupal Developer

ptocheia’s picture

This works perfectly, thanks for posting this!

Tino’s picture

I'm also looking for a solution for this problem. Imho this should definitely be a standard feature in Views, without the need for PHP coding.

For this solution to work in Drupal 7 <?php and ?> should be left out:

$node = node_load($argument);
return (is_array($node->field_fieldname) && count($node->field_fieldname) > 0);

Doing it that way it "works". But on every page where the view (block) has no results, I get an error message:

Notice: Undefined property: stdClass::$field_fieldname in eval() (line 2 of ../sites/all/modules/views/plugins/views_plugin_argument_validate_php.inc(53) : eval()'d code).

How can we fix this?

Tino’s picture

Dropped the PHP code solution on the contextual filter completely.
Instead I added a filter criterium to the view:

Content: Field fieldname:fid (not empty)

Solved! :)

gaëlg’s picture

Yes, this works great.

giorgio79’s picture

elvismdev’s picture

This worked better for me, no php notices dropped to log and block totally hidden if no content to show. ;)

Anonymous’s picture

Thk Tino for great tip!

madelyncruz’s picture

Works perfectly and less work.. Thank you much Tino... I don't have to use any javascript just to hide the block when the field is empty :)

justinthughes’s picture

Thank you for this!

teedave’s picture

For some reason "added a filter criterium to the view" didn't work for me. It could be because I have 2 fields (see below comment for that scenario).
I just added an isset

$node = node_load($argument);
if (isset($node->field_fotos) && is_array($node->field_fotos) && count($node->field_fotos) > 0) {
  return TRUE;
}
else {
  return FALSE;
}

Hope this helps someone.

andymark.bel@gmail.com’s picture

thanks for helping, this is all what i needed

ashopin’s picture

This worked for me, thanks!

Erik Olson’s picture

Field (not empty) worked for me. Thanks a bunch!

bryanmanalo’s picture

Worked for me. Thanks. I don't like to unnecessarily add php in views

dlaufer’s picture

@tino Worked for me! Not sure why I was overthinking it!

Geert_Everardus’s picture

Thanks Tino! 7 years later still superhelpful! :D 

themorebeautifulworld’s picture

@nicoz steps did not work for me. That made the block disappear from everywhere.

@scotself idea to rearrange the filters did not work. Same problem.

@tino step to include a filter criteria for Content: Field fieldname (not empty) DID WORK. Thank you!

sebby’s picture

thx @halth

It's working perfectly with a view using a block display.
his argument is content ID from URL.

wooody’s picture

Thank you halth

bepnhahang’s picture

Thanks. I'm working it.

lesleyfernandes’s picture

Tks @halth

Lesley Fernandes Moreira
Web Consultant | Drupal Developer | Entrepreneur
www.lesleyfernandes.com

blamege1’s picture

How would I accomplish this with a view that has 2 fields? The above code works for one field, but I'm not a PHP guy, so I'm not sure how I would edit that solution to reflect two separate field values. For instance, I have a view with an image and a summary. I need it to display when I have 1 of those values or 2 of those values, but not display when I have 0 values available. Any help would be greatly appreciated.

A little edit to my original post. Hope this helps…

This works great when their is only one field

$node = node_load($argument);
return (is_array($node->field_fotos) && count($node->field_fotos) > 0);

how to do this with field_fotos and field_summary?

teedave’s picture

I had the same request and solved it by:

$node = node_load($argument);
if ((isset($node->field_fotos) || isset($node->field_summary)) && (is_array($node->field_fotos) && count($node->field_fotos) > 0) || (is_array($node->field_summary) && count($node->field_summary) > 0)) {
  return TRUE;
}
else {
  return FALSE;
}

Note that these fields are either or, not both required, so if one has a value and the other doesn't, it will show the one it has. To make them both required, change the or's - || - to and's - &&

gmaxwelled’s picture

You can also do this in the Views interface by adding the two filter criteria and then clicking the down arrow and the "And/Or, Rearrange" button. Then click "Create new filter group" and drag both your field conditions into it. Finally, change the operator in the new filter group to "Or".

This works as an inclusive OR. You might need to use code or add more filter groups if you want an XOR.

jppi_Stu’s picture

Thank you, @gmaxwelled -- this was a clean solution with no quirky, custom coding or obscure, non-obvious settings. I just did this in Drupal 8 and finally got the view/block to disappear when none of a set of five optional fields had data. The only grief along the way was that dragging and dropping the fields to the second filter group failed miserably (something I may submit as a bug report if I can document it well enough). I had to show row weights and move the filter fields to the second group that way. Once I did that, it was smooth sailing.

amuli’s picture

Thank you very much Tino and Gavin : works like a charm :-)

ranjeesh’s picture

Thank you very much...after a couple of weeks of wondering and pondering I found your solution very helpful...not sure why I didnt find this earlier.

ramonoak’s picture

This give-me the solution for my problem, thanks a lot dude!

rroose’s picture

Thanks! This works.

jeffrod’s picture

Is this answer still accurate? This contextual filter is hiding the block regardless of what data the query returns. In fact, the preview panel says "Query was not run." It appears this doesn't concern itself with the query. It always hides the block.

My understanding is that when the query returns no data, this CF will hide the block. When the query returns data, this CF would be inactive and the block would be visible..

shobhit_juyal’s picture

It saved my time.

lroedal’s picture

Thank you for this, very helpful! :)

wouterb038’s picture

Ik have the same problem...

Rearange is not the solution

Can somewhan please help me

mortona2k’s picture

If I understand this technique, the view is only hidden when there is no contextual input? In my case, the input is valid, but there are no results. How do I hide any block content (wrappers) from being displayed?

The other Andrew Morton

enrique.delgado’s picture

I also have the same problem. A view displayed inside a block. Nothing to show in the view, but the block is there, just showing it's title and then empty inside.

shopoftheworld’s picture

I figured out how to do this without using PHP validation combining the techniques above.

This is for a related links view that is relevant to the current node (so may not work for other scenarios) sometimes there are related links - sometimes not

Create your view as a block
Using a contextual filter - filter by nid and provide a default argument of ContentID from URL.
Provide an override block title in the contextual filter for when a default is provided.
Make sure your block has NO Title (not )

Add a field filter against one of the fields you want in your view to make sure its not empty like this

Content: Related links:title (not empty)

That worked for me

Thanks

lovelykaushik86’s picture

Thanks a lot for your solution It saved my time

nasia123’s picture

this works, it is the best solution provided in this thread

GotYourGuy’s picture

Thanks shopoftheworld, your solution worked for me.

rakesh.gectcr’s picture

Yeah!, its working.

---
Kind regards,

Thank you,
Rakesh James

thomasmuirhead’s picture

Thanks for the specify validation criteria tip @heitoralthmann - really really helpful.

I was having this problem with a view of referenced nodes. I used it to hide the view if there were no nodes referenced. Had been a pain having the title showing without anything in it before that.

nickbumgarner’s picture

Good information, thanks OP!

Nicholas A. Bumgarner
Web Developer
Inclind, Inc.

Ahmad Sawaie’s picture

Hide block if view is empty

from Filter criteria--> Add-->Filter--->Global---> Global: Fields comparison .And Select the condition

jordimateubesancon’s picture

Not exactly the same case but similar. What I need is to hide content pane when the view has no results included the title of the view (by the way the view has a contextual filter Content: Nid that takes the argument form the url). The easiest way I've found to fix it is to add the fields of the view as filters and select the Is not empty (NOT NULL) operator as filter criterion for each one.

game’s picture

I have the same scenario but for a multi value field this is not working :-(

jay.lee.bio’s picture

What a pain in the ass. Of all the solutions mentioned here, yours is the only one that worked for me. Thanks!

____________________

https://jay.lee.bio

sillo’s picture

Just what i needed - Thanks alot!

game’s picture

Im having the same issue and cant get this to work for a multivalue field... its driving me crazy :-/ Just need the title to hide with the view and contextual filter. :-(

game’s picture

Just to update I did do this in the end by not having a title set and then used the rewrite field option to add the title in above the content... just had to stop and think for a moment :-)

niklp’s picture

Something I never knew in views. TYVM to the original commenter.

krystalcode’s picture

For me the main problem was coming from the fact that the "views-view.tpl.php" template renders a wrapping div even if there is nothing to render inside it. A solution that should work in all cases is to override the default template and conditionally render the template only if there is something to render. The solution should be generic because it looks not only for content, but for attachments, header or footer, no-results behaviours defined etc as well. It should safely apply to all views that use the theme.

In your theme's template_preprocess_views_view() function (create it if it doesn't exist) add the following:

function MYTHEME_preprocess_views_view(&$variables) {
  $variables['content_exists'] = $variables['title_prefix'] ||
    $variables['title'] ||
    $variables['title_suffix'] ||
    $variables['header'] ||
    $variables['exposed'] ||
    $variables['attachment_before'] ||
    $variables['rows'] ||
    $variables['empty'] ||
    $variables['pager'] ||
    $variables['attachment_after'] ||
    $variables['more'] ||
    $variables['footer'] ||
    $variables['feed_icon']
  ;
}

Copy the views-view.tpl.php template from the views module (in the sites/all/modules/views/theme folder) and add it to your theme folder. Wrap the whole template (after the comments section) with the following code:

<?php if ($content_exists): ?>
// Template content here ...
<?php endif; ?>

Clear your caches so that the new template and preprocess function are loaded.

umeshpatil’s picture

I don't want to write PHP in view as I dont feel its good(this is purely my view, i just don't like writing PHP in views).
So I tried other solutions from above list but they did not work for me except the only solution worked for me is jordimateubesancon's TYSM for that.
Let me explain what I did for those who will ran into this problem.

1. Add a FILTER CRITERIA
2. Select the same field that you are displaying under fields
3. Select the Operator as "Is not empty(NOT NULL)"
4. Save it and its done.

Cheers!! ;)

walking-bird’s picture

Just wanted to say that this solution worked for me, thank you very much!

slown’s picture

Thank's a lot umeshpatil, it works perfectly in this way.

shuva.15’s picture

Thanks a lot umesh.
Perfect solution without the involvement of code.
Once again, thanks.

maxplus’s picture

Hi,
adding a filter and moving the view title to a textfield in my view header, solved my problem.
(if not moving the view title to the view header, the title is still displayed)

Thanks!

sophiaqb’s picture

Field, not empty is fine for me. Thanks.

vector_ray’s picture

I've create a module based on the solution provided by Krystalcode with some minor tweaks. For anyone interested in trying it Hide Empty View.

justono’s picture

New to drupal still after a year. But just so anyone else looking for how to do this today there is an option on the views. 8.3

kyberman’s picture

If anyone search for it, it's called "Hide block if the view output is empty" and you can find it between "Other" options inside "Advanced" fieldset (only on block displays).

Mytko Enko’s picture

It is only applicable for blocks, any ideas of how to hide view display/menu? 

jared nolt’s picture

I should have read this before experimenting for a while. One checkbox, and the block disappears!

bmyoung’s picture

Options for a view block cannot be accessed when ctools 3.0 is installed. The issue was fixed in https://www.drupal.org/project/ctools/issues/2820783 but you need the ctools dev release to include the fix.