检查一个区域是否被占用

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;
Drupal 6

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

Drupal 7

 
<?php
 
if($page['sidebar_first']) {
   
// 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 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;
}
?>