Saturday 30 May 2015

How to check whether a region contains block for current page?

At some cases, we might need to add some div structures to a group of regions, at that case, we need to check whether region has any block or not to place the div structure.
 
Ex:
Code in page.tpl.php
 
<?php if(region_empty($testRegion1)) { 
?>
<div id="testId">
<?php
print $testRegion1;
 print $testRegion2;
 print $testRegion3; 
 print $testRegion4;
?>
</div>
<?php 
}
?>
The function region_empty() would return 1 if it has a block to show.
<?php
function region_empty($test_region) {
  $test_empty = 1;
  $result = db_query_range('SELECT n.pages, n.visibility FROM {blocks} n WHERE n.region="%s" AND n.theme="%s"', $test_region, $GLOBALS['theme'], 0, 10);
  if (count($result) > 0) {
    while ($node = db_fetch_object($result))
    {
 
      if ($node->visibility < 2) {
        $path = drupal_get_path_alias($_GET['q']);
 
        // Compare with the internal and path alias (if any).
        $page_match = drupal_match_path($path, $node->pages);
        if ($path != $_GET['q']) {
          $page_match = $page_match || drupal_match_path($_GET['q'], $node->pages);
        }
        $page_match = !($node->visibility xor $page_match);
      } else {
        $page_match = drupal_eval($block->pages);
      }
 
      if ($page_match)
        $test_empty = 0;
    }
  }
  return $test_empty;
}
?>

No comments:

Post a Comment

Your comment is so valuable as it would help me in my growth of knowledge.