By Polyspiral on
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
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
1. Go to the advanced tab of
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'
If I could "like" this, I would!
Thank you nicoz, this saved me!
Make that two likes!
Make that two likes!
Thank You for the hint!!!
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).
rearrange
Switch the order/weight of the filters (click rearrange) and see if that helps?
You should use a validation
@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:
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
It works!
This works perfectly, thanks for posting this!
I'm also looking for a
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
<?phpand?>should be left out: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?
Dropped the PHP code solution
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! :)
Yes, this works great.
Yes, this works great.
Thanks Tino
Thanks Tino
My Drupal sites:
Perfecto!
This worked better for me, no php notices dropped to log and block totally hidden if no content to show. ;)
You win a beer !
Thk Tino for great tip!
Works perfectly and less work
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 :)
+1 to Tino
Thank you for this!
Or use an isset?
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
Hope this helps someone.
it's Help!!!
thanks for helping, this is all what i needed
This worked for me, thanks!
This worked for me, thanks!
Field (not empty)
Field (not empty) worked for me. Thanks a bunch!
Worked for me
Worked for me. Thanks. I don't like to unnecessarily add php in views
Tino +1
@tino Worked for me! Not sure why I was overthinking it!
Elegant solution
Thanks Tino! 7 years later still superhelpful! :D
@nicoz steps did not work for
@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!
thx @halth It's working
thx @halth
It's working perfectly with a view using a block display.
his argument is content ID from URL.
Thank you halth
Thank you halth
use a validation
Thanks. I'm working it.
Tks @halth
Tks @halth
Lesley Fernandes Moreira
Web Consultant | Drupal Developer | Entrepreneur
www.lesleyfernandes.com
Two fields?
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
how to do this with field_fotos and field_summary?
Two fields
I had the same request and solved it by:
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 - &&
Two fields
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.
Perfect (well, almost)
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.
Thank you very much Tino and
Thank you very much Tino and Gavin : works like a charm :-)
Awesome
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.
Thanks!
This give-me the solution for my problem, thanks a lot dude!
Thanks! This works.
Thanks! This works.
Still accurate?
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..
Thank you
It saved my time.
Thank you for this, very
Thank you for this, very helpful! :)
Help
Ik have the same problem...
Rearange is not the solution
Can somewhan please help me
If I understand this
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
Did you ever figured it out?
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.
Hiding a view block with no content
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
Thanks a lot for your
Thanks a lot for your solution It saved my time
it works!
this works, it is the best solution provided in this thread
Thanks shopoftheworld, your
Thanks shopoftheworld, your solution worked for me.
Thanks nicoz
Yeah!, its working.
---
Kind regards,
Thank you,
Rakesh James
Thank you so much
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.
Hooray!
Good information, thanks OP!
Nicholas A. Bumgarner
Web Developer
Inclind, Inc.
Hide block if view is empty
Hide block if view is empty
from Filter criteria--> Add-->Filter--->Global---> Global: Fields comparison .And Select the condition
Hide view's title in a panel when the view is empty
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.
I have the same scenario but
I have the same scenario but for a multi value field this is not working :-(
GAMe
http://www.aprox.co.uk
What a pain in the ass. Of
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
Saved my ***
Just what i needed - Thanks alot!
Im having the same issue and
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
http://www.aprox.co.uk
Just to update I did do this
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 :-)
GAMe
http://www.aprox.co.uk
Something I never knew in
Something I never knew in views. TYVM to the original commenter.
Alternative solution
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:
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:
Clear your caches so that the new template and preprocess function are loaded.
Easiest Solutions to achieve this
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!! ;)
Easy and it works
Just wanted to say that this solution worked for me, thank you very much!
Work great for me ...
Thank's a lot umeshpatil, it works perfectly in this way.
Thanks a lot umesh.
Thanks a lot umesh.
Perfect solution without the involvement of code.
Once again, thanks.
Hi,
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!
Simple and good one
Field, not empty is fine for me. Thanks.
A module based solution...
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.
There is an option Today
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
If anyone search for it, it's
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).
How to do it with view page, not block?
It is only applicable for blocks, any ideas of how to hide view display/menu?
This is the answer for Drupal 8!
I should have read this before experimenting for a while. One checkbox, and the block disappears!
If using ctools 3.0 you need the dev version
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.