Show Drupal blocks only on specific pages with PHP code
Monday, November 9, 2009Drupal 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 here.
- og_is_group_member(): Check a user's membership in a group. More here.
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.
Nepal is hit hard by climate change, lets go green - Blog Action Day
Thursday, October 15, 2009In the past years Nepal has been hit very hard by the impacts of climate change. Being a land locked country and with places ranging from almost the sea level to the highest place on the planet, the country has great biodiversity and breathtaking landscapes. But rising pollution levels in the cities and low awareness about impacts of climate change has taken its toll mainly on the people who live in and near by cities.
A prominent problem Nepali is facing is global warming, due to which the glaciers are melting at an alarming rate. The whole himalayan range and the region is in danger due to the fast melt down of the ice in the region. The region is facing rise in temperature and it is adversely affecting the lifestyle.
The agriculture sector has also been hit badly , due to the climate change previously nearby Kathmandu (the capital city) farmers could grow apple. But now due to the climate change farmers can't grow apple as the temperature has risen in the past decade. Other agriculture products have also change in nature, a country that depends very much on agriculture this is big loss. Nowadays, plants like mango that did not grow in Kathmandu being cooler grows here due to the rising mercury. Mangoes only grew in terai (the hotter region) but now it grows in and around Kathmandu which is not a good sign.
I truly hope we all can come together and tackle the global problem of climate change. It may be growing problem but small things we do can reduce the effects :). Support Blog Action day 2009.
Amazing High dynamic range (HDR) photos by Prashant
Wednesday, October 14, 2009YoungInnovaitons Private Limited is a company consisting of talented people. Among them Prashant Shrestha our own XHTML/CSS guru can work his hands greatly on the camera mainly the single lens reflex (SLR). He recently tried his hands on High Dynamic Range (HDR) photography. See below and image that depicts the beauty of HDR.
Wikipedia defines HDR as "In image processing, computer graphics, and photography, high dynamic range imaging (HDRI or just HDR) is a set of techniques that allow a greater dynamic range of luminances between the lightest and darkest areas of an image than standard digital imaging techniques or photographic methods. This wider dynamic range allows HDR images to represent more accurately the wide range of intensity levels found in real scenes ranging from direct sunlight to faint starlight." And the images below gives you the feel what its all about.
Hope you like the pictures taken by the man himself - Prashant Shrestha. check out his flickr page at http://flickr.com/photos/prashant_sh.












