Last updated May 31, 2012. Created by lukus on July 13, 2009.
Edited by randomblink, DSquaredB, pavelsof, Timothy Sharpe. Log in to edit this page.
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, node and view levels.
To deal with this case, part of the block.module code could be adapted 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;
}
?>
Comments
So this goes in the D6 theme
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
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
I think it should work with PHP statements too .. but your php statements need to be enclosed in
<?php..
?>
w
w
Not sure if this is a safe way to achieve the same thing...
This seems to have worked for me.
In order to bypass what you outlined here:
I use the following in my template.php file. putting this in my template.php file allows me to call regions within node and block templates as well as check to see if the regions are occupied on node and block template files.
function mytheme_preprocess_node(&$vars, $hook) {$vars['region_to_check'] = theme('blocks', 'region_to_check');
}
It works for me, but I'm not sure if this is a security issue? It also seems to take into account whether or not the region is allowed to display on that page. I'm not a drupal pro, but I think all this does is process the region's content before the processing of the node/block that is trying to figure out whether its populated.
Guess the code is for D6.
Guess the code is for D6. Made a quick port to D7 but it always return 0.
<?php
function region_empty($test_region) {
/* Check to see if a region is occupied
* returns 1 if it's empty
*/
global $theme;
$test_empty = 1;
$result = db_query_range('SELECT n.pages, n.visibility FROM {block} n WHERE n.region=:region AND n.theme=:template', 0, 10, array(':region' => $test_region, ':template' => $theme));
foreach($result as $node) {
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;
}
?>