Skip to main content
Drupal blocks are the basic building elements of any Drupal page. Blocks are placed in block regions and they serve different purposes from showing a list of elements even to show a picture or a video.
Menus of the Drupal system are also blocks of special kind that show links to other drupal nodes/pages/url or external links. (I assume that you know about drupal blocks and how they can be shown or hidden on basic given conditions and also you have some knowledge of php.)

Having a look at a block's configuration page (Site Building >> Blocks >> List) and clicking configure aside one of the block descriptions there are various ways to control the visibility of a particular Drupal block.Some are let users control what they see, to show only to block to only specific roles.

The Need

In page specific visibility the first two options are quite clear, you want to show a particular block in some url and its sub URL like setting to option 2 : "Show on only the listed pages." and typing the following on pages:
  • blog - will show the block on siteURL/blog page only
  • blog/* - will show the block on siteURL/blog/any url - pages
But what if I want to show a block in one node type (or content type), then the need to choose the third option "Show if the following PHP code returns TRUE (PHP-mode, experts only)." arises.  It does not need expert knowledge.


Scenario 1: For node types

After selecting the third option on "Page specific visibility settings", the logic is quite simple if TRUE is returned the block is shown if FALSE is returned the block is hidded from display. Lets say I want to see a the block only on story content type, then I can do it by pasting the code below on the PHP code option in page specific visibility settings.

  <?php
// Only show if $return is true
$return = FALSE;

// Match current node type with story
if ( (arg(0) == 'node') && is_numeric(arg(1)) ) {
$nid = arg(1);
$node = node_load($nid);
$type = $node->type;
if($type == "story") {
$return = TRUE;
}
}

return $return;
?>

In action:

You can further enhance it with multiple node types with use of the in_array() php function to show multiple node types. Or tweak it to fit your custom needs.

Scenario 2: For OG (Group) nodes

I had yet another problem I needed to show some specific blocks like group calendar, group blogs only to OG - Group nodes and only to group members. So I digged into the OG module and found two very good functions, they are:
  • og_get_group_context(): Used for things like setting current theme and breadcrumbs.  - Check og module for more information or see it on drupal docs.
  • og_is_group_member(): Check a user's membership in a group. More on Drupal docs.
So using these two functions I can show a block only when a group node is loaded and only to the group's members with the code below:

<?php
$return = FALSE;

if ($groupnode = og_get_group_context()) {
if(og_is_group_member($groupnode->nid)) {
$return = TRUE;
}
}

return $return;
?>
The above code could have been better :). Above code should also be put in the "Page specific visibility" of the block configuration settings page.

Better Use and Possibility

A better use of the above code I assume will be to make a module and call a function. The function will determine to show the blog or not. It will give a central control of the code else if one thing is changed like in scenario one if node type has to be blog from story it should be edited in all the blog config pages which is a very bad solution. So calling a function form lets say for now a custom_show_block module with function: custom_show_block_normal() will do the trick. Below is a screenshot in assumption that custom_show_block module has been created and custom_show_block_normal() function in custom_show_block module has code of scenario 1:


If the module will have setting to choose the node types and other things it would be great ;).

Conclusion

Block visibility with PHP is very flexible and customizable like other parts of Drupal. With block visibility for specific pages with PHP the need to show a block for specified condition will be easier. Happy coding.

Comments