Drupal: Show/Hide Block On Forum
Here you will learn how to show or hide a block on a forum. The instructions include as How-To info for the whole forum (forum/*) as well for its pages in viewing or editing mode.
Solution
In a block settings, check “Show if PHP-code returns TRUE (PHP-mode, experts only).” and enter one of the following code pieces below.
DON’T SHOW a block on forum PAGES:
*A block will be visible on pages with ‘forum/*’ URL
$result=true;
if ((arg(0) == ‘node’) && is_numeric(arg(1))) {
$node = node_load(arg(1));
if ( $node->type == “forum” ) $result=false;
}
return $result;
This code returns TRUE only if ‘forum’ node isn’t viewed this time. So a block is shown always except ‘forum’ node is currently opened.
Show a block ONLY on forum PAGES:
*Shows a block only on a forum pages (this includes viewing and editing of a forum nodes)
if (arg(0) == ‘forum’) return TRUE;
if (arg(0) == ‘node’ && is_numeric(arg(1))) {
$result= db_fetch_object(db_query(“SELECT n.type FROM {node} AS n WHERE n.nid=%d LIMIT 1″, arg(1)));
if ($result->type == ‘forum’) return TRUE;
}
return FALSE;
DON’T SHOW a block everywhere on a forum
*A block will be hide on a whole forum (this includes viewing and editing of a forum nodes)
if (arg(0)==’node’ && is_numeric(arg(1))){
$result= db_fetch_object(db_query(“SELECT n.type FROM {node} AS n WHERE n.nid=%d LIMIT 1″, arg(1)));
if ($result->type == ‘forum’) return FALSE;
} elseif (arg(0)==’forum’) return FALSE;
return TRUE;
DON’T show a block when viewing or editing a topic only:
if (arg(0) == ‘node’ && arg(1)){
$result= db_fetch_object(db_query(“SELECT n.type FROM {node} AS n WHERE n.nid=%d LIMIT 1″, arg(1)));
return ($result->type != ‘forum’);
}
Showing a block when viewing or editing a ‘forum’ node is your homework =)