Is there a way to hide a block that's showing a view when the view is empty? I set the empty text to none, and there is a similar function that hides the block if it is not applicable to the current page - but I can't see a way to hide the block if its result is empty.

Comments

merlinofchaos’s picture

If you have no empty text, blocks should hide themselves if they return no results unless has something like the calendar style.

eagereyes’s picture

The empty text is empty, but as soon as I set a title in the view or in the block, it always shows. Is that the problem? How else can I set a title? I'm close to just doing the title in the theme, but it just strikes me as ugly to do it that way.

merlinofchaos’s picture

Category: support » bug

When I have more time I'll see if I can duplicate this; but if the block isn't hiding itself when it should, it's a bug.

Fayna’s picture

I'm having the same problem. Images from an imagefield are supposed to display and when there's none to display for a particular node, it just displays the title of the view or block, nothing else. If I configure empty text it doesn't show up. I thought maybe I was missing something else until I found this issue!

merlinofchaos’s picture

Note that a field not having data is not the same, from Views perspective, as there being no rows returned.

gustav’s picture

Status: Active » Needs review
StatusFileSize
new808 bytes

Attached is a patch that fixes this bug. The problem was that the view template returned something non-empty even if the view was empty and there was no empty text.

merlinofchaos’s picture

No, this is wrong. It's a very specific case that blocks should hide; this patch causes pages that have no results to display no result either, and that's definitely not the right solution. There is a specific check for blocks to hide. Maybe it's failing but last I tested it it worked.

gustav’s picture

If pages that have no results display no results that seems right, doesn't it?

Where is this specific check for empty blocks that you mention? I have not been able to find it in the code and it is definitely not working for me.

merlinofchaos’s picture

No, because returning NULL causes the page not ot display at all, and you whitescreen. That's bad behavior.

gustav’s picture

I would like to help to fix this bug, but a hint about where the code is that is supposed to deal with empty blocks would help me.

eagereyes’s picture

As I said above, I might be doing something wrong - but I've tried this again in the meantime with different combinations of settings, and none of them work correctly. I'd very much appreciate a solution to this.

I guess the point where the change needs to be made is where the view decides to display the "empty text" instead. I haven't actually looked at that myself, but that seems to be the point where that same decision is made. I just don't know if it can turn the node off at that point.

merlinofchaos’s picture

Status: Needs review » Closed (won't fix)

Ok, I just tested this.

I created a very simple view. I gave it a filter that cannot pass (Specifically: nid == 0). I gave it a block view, and I put that block into my system.

The block does not appear.

The code that prevents the block from displaying exists in views_plugin_display_block::execute() which very specifically will not display the view if it has no results, no empty text, and does not have 'even empty' set in the plugin definition, which the base block does not.

The only way your view should be appearing is if you

1) have an empty text set (even a space will do it, and it's possible wysiwyg editors might put some invisible markup here that could cause the empty text to appear to be populated), 2) have a result that isn't actually displaying anything because all of the fields it would display are NULL.

But from my testing, this feature is working just fine.

gustav’s picture

Merlin, thank you very much for that very helpful response. I learned from it that the views module looks at $view->result to determine whether a block is empty, rather than looking at whether the content is empty. This allowed me to fix a bug in the tagadelic_views module which had a non-empty $view->result even when the block had no content.

Fayna in comment #4 probably has a similar problem that her block output is empty even if the list of nodes that are pulled up by the views module is not empty. I don't know how to circumvent the problem in her case though.

merlinofchaos’s picture

The answer to that problem is usually to provide proper filtering so that records with no data will not be considered results in the query.

eagereyes’s picture

Category: bug » support
StatusFileSize
new2.44 KB

How do I know that nodes are returned when I don't see them? I'm attaching the view, it's very simple: all it does is look for nodes that are associated with the current one with a Node Reference field named field_related. There is no filter, and I don't see how I can filter this list. If there are no entries, it must be empty!

I also just tried setting an empty text, and it doesn't show it when there are no results. Something's wrong here, and I would really like to know what.

merlinofchaos’s picture

Simple: Add Node: Title as a field. That one will have data, even if your field happens to have NULL. Remember, just because the fields are NULL does not mean a record was not retrieved from the database.

eagereyes’s picture

Category: support » bug
Status: Closed (won't fix) » Active

It seems to me that Merlin's test case doesn't apply to any non-trivial use case. Here's why.

The following queries are run by the view when the node has no related nodes. This first one of course finds the current node, and thus has an output in a sense. Merlin's example doesn't. But if this is the reason the block is shown even though there is no visible output, I'd say that's a bug.

SELECT node.nid AS nid, node.vid AS node_vid, node.type AS node_type FROM node  WHERE node.nid = 505

I don't know what the vid is, and why it's not using the nid for the next query. But the first query in that next block is what counts, and that has zero rows output (I tried). The rest also produces output, but is just additional stuff that's of no relevance for the output (and I don't see what it's being queried for in the first place).

These queries were run during view rendering:
[0.49 ms] pre_render /* pre_render */ SELECT node.vid AS _vid, node_data_field_related.delta as _delta, node.nid AS _nid, node_data_field_related.field_related_nid AS nid FROM content_field_related node_data_field_related LEFT JOIN node node ON node.vid = node_data_field_related.vid WHERE node.vid IN (506) ORDER BY node.nid ASC, node_data_field_related.delta ASC
[0.55 ms] render_textarea /* render_textarea */ SELECT name FROM filter_formats WHERE format = 1
[0.24 ms] drupal_lookup_path /* drupal_lookup_path */ SELECT dst FROM url_alias WHERE src = 'admin/build/views/edit/relatednodes' AND language IN('en', '') ORDER BY language DESC
[0.11 ms] drupal_lookup_path /* drupal_lookup_path */ SELECT dst FROM url_alias WHERE src = 'admin/build/views/export/relatednodes' AND language IN('en', '') ORDER BY language DESC
[0.11 ms] drupal_lookup_path /* drupal_lookup_path */ SELECT dst FROM url_alias WHERE src = 'admin/build/views/clone/relatednodes' AND language IN('en', '') ORDER BY language DESC

So I'm switching this back to active for you guys to decide. If somebody wants to look at my view, it's posted above. If anybody has any concrete input how to change it to work as intended, that would be very welcome. But until then, I'm considering this a bug.

merlinofchaos’s picture

I am seriously going to go apopleptic here, because I am telling you what is wrong and I feel I am being deliberately ignored.

ADD SOME FILTERS TO YOUR VIEW TO REMOVE THE ROWS THAT HAVE NO VALUE.

Is this that hard?

The fact that there are additional queries being run PROVES that the view is getting results back. It's just that you are not displaying any fields that have results. The view you attached HAS NO FILTERS. So it will pull up whatever record the argument said to.

Let me quote myself, because you really, really, really need to read these sentences:

Simple: Add Node: Title as a field. That one will have data, even if your field happens to have NULL. Remember, just because the fields are NULL does not mean a record was not retrieved from the database.

The check cares about records being retrieved. The check does not give a rat's ass if you have fields that all have NULL. It does not KNOW and it does not CARE. You MUST add filters to remove these records. END. OF. STORY.

This is not a bug, this is you being too stubborn to actually pay attention to what I say, which I have to say, is upsetting on several levels. People who ask for my help, refuse to listen, argue with me, and then try to tell me that it's a bug are serious wasters of my time.

eagereyes’s picture

We're not talking about the same thing, apparently. I added the node's title as a field, but I didn't see a way to make use of that. I can display the node title, but I don't want that. How am I supposed to use it for filtering? It's always going to have a value unless I specify nid = 0.

The first query returns a result, but it's useless to me. What I'm trying to do here is get a list of related nodes, and not to show anything when there aren't any. Instead of writing your abusive rant about how dumb I am, you could have told me how to do that.

merlinofchaos’s picture

Category: bug » support
Status: Active » Closed (won't fix)

There isn't a 'first query'. It's all one query, which is my whole point. The fact that Node: Title has data tells you that a result was returned, and it's just that fields you're looking at don't have data.

There are many ways to to apply filters. Try adding Node: Nid as a filter, setting it to the relationship, and set the "is not empty" filter. You could also edit the relationship and set 'require this relationship', which I think appears on the node reference relationship. That will require there is actually a referenced node. You could also try adding a node: type filter, setting it to the relationship, and filtering that to only nodes that are valid in the relationship. Obviously that only works if your data is set up so that the related nodes are of different types.

There are probably other filtering options that would work as well, depending upon your data.

manuel garcia’s picture

StatusFileSize
new23.12 KB

just for the record, here's how I fixed my recent commetns block from showing up with empty markup:
Added an extra filter for comment title not equal to NULL -- see the attached image to take a look.

friolator’s picture

I have a situation where I can't seem to come up with a filter scenario that works, and as a result blocks with no results are displaying.

We have Product pages on our site, and a block generated by a view (displayed through Panels) that shows all of the images on the site that are tagged as being associated with that product. There are two content types from which the images are pulled - the user's content profile and reviews that the user has posted. So the view is really pretty simple:

Argument is a taxonomy term

fields:
Photo 1
Photo 2

Filters are:
Node Is Published
Node is of Content Type "user_profile" or "review"

And this works perfectly as long as there are results. The problem is that when there are no results, we get the blank block. The only way I've been able to make the block go away when there are no photos is to add a filter for one of the Content: Photo types, and set it to "True" so that it only works if the photos are there.

However, there doesn't seem to be a way to filter for Photos that are of *either* type. because each one is an independent filter, if you don't have photos in one but you do in the other, the view comes back with empty rows, because it's looking at Filter 1 AND Filter 2.

Is there any way to specify an "OR" in this scenario?

Alternatively, what about the option to consider the view "empty" (or something similar) in the case where no results are returned?

friolator’s picture

sheesh. nevermind. I finished reading through this thread. The solution is a filter that looks for a node id > 0

works perfectly now.

i think i need to finish my coffee...

glennnz’s picture

Yup, y'all need to read what merlin says...

I had this problem, just added filters for Published = Yes, and voila!!

macrodesign’s picture

Thanks. works for me. I had a content / image block, always empty on other nodes. simple filter (true) works perfect

macrodesign’s picture

sorry it did produce unwanted results later on, but i managed to fix it by adding another different filter such as that check the node type.

a6hiji7’s picture

Title: How can I hide the whole block if its view is empty? » How can I hide an empty row?
Version: 6.x-2.1 » 6.x-2.11

Hi,

Is there a way to hide a views row (in list style view) if all the fields are empty? I have a view that has only field. I have checked "Hide if empty" in field's setting. This works fine but the row (the li element) still displays. Since my li elements have CSS styles applied, they don't look good with empty fields. Is there a fix for this?

manuel garcia’s picture

Use the not null filter on that field a6hiji7 like the screenshot on http://drupal.org/node/332762#comment-1848244

a6hiji7’s picture

Thanks for the reply. My problem is a bit more complicated as I am using views customfield. Anyway, I got through the problem by overriding template_preprocess_views_view_list in my template.php file. Now the items don't show but the block still shows with the title (EDIT : even if there is no row).

zydu’s picture

Hi, I have a similar problem

I created a new content type and added a field named photo_field to it. Photo_field can store up to 8 images, default image is not provided. I then created a block view which displays these images in a grid. Added node:type and node:published filters, but block is displayed even if there are no images (I actually filled in "Empty text", but that message is never displayed as if there were results). I tried adding Content: Photos (field_photos) - delta is not empty (NOT NULL) filter but it doesn't help either.

Any ideas how to fix this?

EDIT: Adding Content: Photos (field_photos) - list:TRUE filter solved it. False alarm...

mastoll’s picture

StatusFileSize
new25.56 KB

Manuel Garcia, I tried setting the comments:title filter to NULL as illustrated in your screenshot. It didn't work.
I have the content:author field in addition to the comment:title field. 'Thought I might need to set a filter=NULL for it as well (fishing for a solution). This also didn't work. I'm doing something wrong, but can't figure it out! I've attached a screenshot of my view. Are there some obvious details that I'm missing?

tajindersingh’s picture

Thanks merlinofchaos,

Worked for me

tajindersingh’s picture

Hi zydu,

I had same problem with my imagefield in view.

That got solved by adding filter such as:
field_photos_list=true

best of luck

Anonymous’s picture

@ Merlin and comment #18,

I've been working a TON on our (soon to be) new company website and I had a block that was being persistent in showing itself when it shouldn't because the fields were empty (or NULL).

I just want to say thank-you for this post.

I knew what you were saying but when you work on a project for a long time you tend to forget "that one stupid little thing" but then somebody smacks you in the head. Thank you for all your hard work and contributions to the Drupal community. Oh, and specifically thank you for this old post because now I'll get a good night's sleep.

markusa’s picture

It would still be nice that if all the fields displayed in a view are empty that views would recognize this and output nothing at all.