Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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']);
}
share|improve this question
could you let me know how you have created the view? – Mohammed Shameem Jan 22 at 11:17
there is a setting in views to hide block if view empty, isn't it? – SGhosh Jan 25 at 11:13

1 Answer

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.

share|improve this answer
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 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 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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