Checking to see if a region is occupied

While theming page.tpl.php it's possible to check to see whether a region is empty, by checking the content of the relevant variable which contains the region's contents.

For example;

<?php
 
if($left) {
   
// do something
 
}
?>

However, region variables haven't been defined for templates at the block and node and view levels.

To deal with this case, I adapted part of the block.module code to create a function which can be inserted in your theme template.php file.

The function takes one parameter (a region name), and returns 1 if the region is empty or 0 if the region is occupied. The function examines the block visibility setting for the current path to to work out if the region is occupied.

<?php
function region_empty($test_region) {
 
/* Check to see if a region is occupied
   * returns 1 if it's empty
   */

 
$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);
        }
       
// When $block->visibility has a value of 0, the block is displayed on
        // all pages except those listed in $block->pages. When set to 1, it
        // is displayed only on those pages listed in $block->pages.
       
$page_match = !($node->visibility xor $page_match);
      } else {
       
$page_match = drupal_eval($block->pages);
      }

      if (
$page_match)
       
$test_empty = 0;
    }
  }
  return
$test_empty;
}
?>

So this goes in the D6 theme

Bartezz - October 7, 2009 - 13:17

So this goes in the D6 theme template.php and then in a block setting PHP code something like;

if(region_empty('right')) {
   return true;
}

If that's the case, it's not working here! :(

Cheers

________________
Live fast die young

Ah, found the reason... The

Bartezz - October 7, 2009 - 13:26

Ah, found the reason... The function takes account of the current path and block visibility settings is not a very accurate description....

It does use the current path... but not block visibility settings if you use PHP statements... only works with paths.

Cheers

________________
Live fast die young

I think it should work with

lukus - November 5, 2009 - 15:26

I think it should work with PHP statements too .. but your php statements need to be enclosed in

<?php
 
..
?>

 
 

Drupal is a registered trademark of Dries Buytaert.