Block visibility in Drupal

Posted by Quinn at August 17th, 2006 11:38am under web 0 Comments Permalink

If you wonder how to control visibiliy of the blocks in Drupal, and especially at the front page of the website, you might want to continue reading this. Otherwise, it's probably boring.

I am using Drupal for the project I am developing at this moment. I wanted the front page to be unique as the most websites out there, I suppose. I thought built-in features in Drupal would give me easy to configure a block to be shown at only the front page. But it didn't. at least, not the way I planned.

Environment: Drupal 4.7.3 with MySQL and Apache

Drupal has admin -> block section to configure blocks individually. At the page to configure a sigle block, there is a section under “Page specific visibility settings”. I thought I just have to put

<front>

in there and everything would be fine.

Well, this surely makes the block to be shown at the front page but also paged front page too. This is not something I had in mind. This kind of configuration might work in many cases but not every time though.

Then, I chose to use “Show if the following PHP code returns TRUE” and PHP code to control the block visibility. What I came to at the first is like below.

<?php
$thisisthefrontpage = FALSE;
if(($_SERVER["REQUEST_URI"] == '/')
 ){
	$thisisthefrontpage = TRUE;
}
return $thisisthefrontpage;
?>

Then, I found another little thing that pager link will give the first page to be this url.

http://mywebsite/node

This page shows only the list of nodes and not the block I want to show in the front page. I first thought this is not the big deal, but then I figured this probably confuse many people who are using this website. I think the url in the above should show the page exactly the same as the front page. So, I changed the block configurations again like below.

<?php
$thisisthefrontpage = FALSE;
if(($_SERVER["REQUEST_URI"] == '/')
  || ($_SERVER["REQUEST_URI"] == '/node')
){
	$thisisthefrontpage = TRUE;
}
return $thisisthefrontpage;
?>

I think this is not really good coding. I wanted to do like

  || ($is_font && !is_numeric($_REQUEST["page"]))

or something like that. But none of them actually worked. I don't know why. Anyway, this is what I came to as the solution at this moment. It may work for you too when you come to similar situations. It would be nice to hear suggestions if you have something in mind. Thanks for reading.

References:
Your Response?