Block Visibility

Published June 15, 2009

I was recently was asked by a client to assist them in creating and displaying a block that would only appear on certain pages within the site. In their case, they wanted to only display the block on node pages - but only when the node was of a particular content type.

Right off the bat, I knew I was going to use the "Page specific visibility" settings on the block's "config" page. Specifically, I knew I was going to have to use the "Show if the following PHP code returns TRUE (PHP-mode, experts only)." option and write a bit of PHP code. Rather then re-inventing the wheel, I did a quick search on found this page on Drupal.org which has a great bunch of snippets for limiting block visibility for a variety of circumstances. While the page was written prior to Drupal 6, most of the PHP snippets appear to still be valid.

While some may argue that this method is less-than-ideal (this type of functionality can also be written into the theme or a custom module in order to keep the PHP code out of the database and under revision control), it is still a valid (and common!) way to get the job done.

There is a patch for Drupal 7 that aims to get the content-type visibility options back into core (it was removed prior to 4.7). If that happens, then this specific type of filtering will no longer be necessary. Get involved and help test the patch to get this into Drupal 7.

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!

Name
CAPTCHA