5

Is there any API to know if the block is empty? Basically what I'm trying to do is to hide/unset the block inside template_preprocess_page.

Here is my currrent code:

 if ($variables['page']['rail_full']) {
    foreach ($variables['page']['rail_full'] as $keys => $blocks) {
      if (isset($blocks['content']['#views_contextual_links_info'])) {
        $name = (isset($blocks['content']['#views_contextual_links_info']['views_ui'])) ? $blocks['content']['#views_contextual_links_info']['views_ui']['view']->name : '';
        $display_id = explode('-', $blocks['#block']->delta);
        $block = views_get_view_result($name, $display_id[1]);
        if (empty($block)) {
          unset($variables['page']['rail_full'][$keys]);
        } 
      }
    }
  }

But the above code always return a count of 1 when I print $block.

The below works fine but not a good solution. I want it to be dynamic in all block as possible.

if (!isset($variables['node']->field_author_favorite_links['en'])) {
    unset($variables['page']['rail_full']['views_franchise_page-block_2']);
}

if (!isset($variables['node']->field_article_column['en'])) {
    unset($variables['page']['rail_full']['views_franchise_page-block_3']);
}
  • could you let me know how you have created the view? – Mohammed Shameem Jan 22 '13 at 11:17
  • there is a setting in views to hide block if view empty, isn't it? – SGhosh Jan 25 '13 at 11:13
6

I had the same behavior with "empty" fields. Unless you don't hide the fields themselves, the design/ markup still renders. Solution for me was to filter for fields being "NOT NULL". (Maybe it depends on the "fences"-module i have installed, too. Not sure, because not tested ...)

2

By default, if a view with a Block display returns no results, it won't be rendered. That is unless you've set some "No Results Behavior" which the view will display when empty. So check to make sure it has nothing set.

enter image description here

So coding shouldn't be necessary unless you're trying to do something more custom. If so, you may want to clarify what it is you're trying to accomplish.

  • 3
    If you're creating a block with a view, then the block will still be rendered in the markup, however the contents will be empty. – Chapabu Jan 25 '13 at 16:23
  • 1
    Actually that's not true. I created a test view with a block display that purposefully returned no results and it wasn't rendered in the region I assigned it to, not even the title. As soon as I modified the block to show results it rendered. – apower Jan 25 '13 at 21:25
  • 3
    @apower If you are creating a block view that is simple then yes you are correct, but if you have one that has a contextual filter and the filter validates correctly, but there are no items in the result set, the block wrapper does still load. – Duncanmoo Oct 1 '13 at 15:05
  • @Chapabu's comments are absolutely correct. I am also running in the same situation – qasimzee Jul 15 '15 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.