PHP block visibility settings

Last modified: March 19, 2009 - 23:16

Drupal 4.7 upwards comes with a new option to determine block visibility; by PHP code. When the code returns the value TRUE, the block is displayed, when the code returns FALSE, the block remains hidden:

<?php
 
if (condition) {
    return
TRUE// block will be shown
 
}
  return
FALSE;
?>

This is of course the verbose equivalent of:

<?php
 
return (condition);
?>

You can enter such code at the configuration page of each block in administer » blocks (admin/block). Configure the block and copy the code in the box below Page specific visibility settings. Make sure to select the last radio button (Show if the following PHP code returns TRUE).

For Drupal 4.6, you need to include the visibility conditions as part of the PHP code that generates the block.

visibility based on nodetype submission...?

anawillem - July 8, 2009 - 19:57

i created a nodetype that is associated with a user's profile vis a vis the Content Profile module. i want to limit a block's visibility based on whether or not they have created that piece of content. so, for example, i have a nodetype called 'am_registration' which is associated to the user who created it. if they have already filled out this content type once, i do not want the block to show. because i only want this block to show up on one page, i have the code for it in the body section:

<?php
global $user;
if (
$user->uid) {
  if (
$user->am_registration) {}
?>

<div id="register-button">
<p><h5>Congratulations! You are logged into the site!</h5></p><p><h4><a href="/node/add/am-registration">To proceed in your registration, please click here.</a></h4></p><p>&nbsp;</p><p>&nbsp;</p>
</div>
<?php
}
?>

it works in terms of becoming invisible when the user is not logged in (if ($user->uid)) but the next embedded if statement doesn't seem to be doing a damn thing...

ideas?

awillem

 
 

Drupal is a registered trademark of Dries Buytaert.