block visibility based on empty results

cindyr - August 11, 2007 - 20:11

I have a sidebar block ("Today's Events") that pulls events from the calendar and just shows today's events only. I have visibility configured by roles, and I want to add another visibity filter that requires results to be present. For instance, my view displays anything with today's date if the node is of type "Event" or "Hot Lunch Menu". Most days there are both, but some days have neither. How do I get the block to not show if there are no events and no hot lunches listed for that day? The view returns an empty string, but the block with the title Today's Events is still there with nothing in it other than the title.

Can someone point me in the right direction? I assume I add custom PHP code, but I haven't a clue how to write it. Something like:
<? php
((event != '') && (hotlunch != '');
?>

Thanks in advance.

(no title)

cog.rusty - August 11, 2007 - 21:01

What I have found is that a block returning an empty string '' does not appear at all, while a block returning even some spaces '   ' does appear as an empty block.

What I do to make it disappear is either return trim($output) (if it is my own custom php block) or else surround the content of block.tpl.php with <?php if(trim($block->content)) { ?> and <?php } ?>.

So, I don't have to use any visibility condition for an "almost empty" result. Now if your empty result returns some html you may have to check for that in your block's code and return an empty string instead.

I am not sure if this behavior is theme-specific. Perhaps someone has a better solution.

Thanks

cindyr - August 11, 2007 - 21:18

That's what I needed! Thanks.

 
 

Drupal is a registered trademark of Dries Buytaert.