I'm trying to set the visibility of a block depending on a profile field (I'm using using the core Profiles module). I found http://drupal.org/node/64330 but I can't work out how to show the block when a particular profile field is empty (it's not a mandatory field so it will be deliberately blank in some cases). It's probably very simple, thanks for any help.

Comments

bdawg8569’s picture

Let me start by saying that i'm not sure that this is a great solution, but here is something you can try. The code you were looking at in the link to your post was for drupal 4.7 and apparently that doesn't work in Drupal 6. I couldn't find any equivalent method so I did a direct DB query to get the profile fields. Mine looked like the following but you will need to edit these based on the name of your profile fields and which field you want to test for.

<?php
global $user;

$result = db_query("SELECT pf.name, value FROM profile_fields pf LEFT OUTER JOIN profile_values pv on pf.fid = pv.fid WHERE pv.uid = $user->uid");
if($result)
{
  $num_rows = mysql_num_rows($result);
   for($i = 0; $i<$num_rows; $i++)
   {
     //get the row
     $row = mysql_fetch_array($result);

     //check for the name of the profile field you want to check
     if($row['name'] == 'profile_firstname')
     {
       //assign the value of the field you want to a variable
       $first_name = $row['value'];
     }
   }
}
//test the value to see if its blank
if($first_name == '')
 return FALSE;
else
 return TRUE;
?>

So you might have to make some small changes to this to get it to work. First of all you need to change the if statement checking the name of the profile field to whichever field in your profile that you want to test. Put this code in the "Page specific visibility settings" section with the "Show if the following PHP code returns TRUE" option selected. Good luck!

Truff’s picture

Hi and thanks for your suggested code. Unfortunately couldn't get this to work, but after further searching I managed to change the first part and got the following to work nicely:

<?php
global $user;
profile_load_profile($user);
$firstname = $user->{profile_firstname};

if($firstname == '')
return TRUE;
else
return FALSE;
?>