I would like to show certain blocks to users only when they are looking at thier own profile, and different blocks when they are looking at someone elses.

I am sure there is a way to do this using the following setting in the block configuration:

"Show if the following PHP code returns TRUE (PHP-mode, experts only)."

I basically need a PHP snippit that does the following:

If username = the username in the path to this profile page
then
show the block

Can anyone help me out here?

Comments

budda’s picture

In the PHP visibility you can check if arg(1) (the user id in the URL) is the same as the currently logged in user $user->uid.

So something like:

global $user;
if($user->uid == arg(1)) {
return TRUE;
}

--
Ixis (UK) providing Drupal consultancy and theme design. Check the portfolio.

nevets’s picture

Something like this may help. Basic logic is to only show block when viewing profile page. By replacing question marks with approriate values can be used to enable block when viewing either own profile or another persons.

<?php
global $user;

if ( arg(0) == 'user' && is_numeric(arg(1)) ) {
$uid = arg(1);
We are viewing a profile page
if ( $user->uid == $uid ) {
// Viewing own profile page
// Return approriate value
// True to view, false otherwise
return ??;
}
else {
// Viewing someone elses profile page
// Return approriate value
// True to view, false otherwise
return ??;
}
}
else {
// Not viewing a profile page
return FALSE;
}

thepaul’s picture

Thank you both for the quick replies, this is a great help. One more question in the same vein:

How would I go about displaying the user->picture in a block on the profile page?

For instance I am viewing userID 1's profile. I would like his avatar to display in a block... if I am viewing userID3's profile I would like her avatar to display in the same block.

Thanks again.

nevets’s picture

Given an user id, this will work

<?php
// You need to set this appropriately
$uid = 1;
$accout = user_load(array('uid' => $uid));
print theme('user_picture', $account);
?>
thepaul’s picture

Thanks for the reply nevets, but I think my issue is a bit more complex because I am using pathauto/url aliases.

I found an old post that deals with the same issue, though I am stll trying to find an aswer!

I will continue my search on that old thread though:

http://drupal.org/node/166818

nevets’s picture

You don't say why it might be an issue but if you are aliasing 'user/N' where N is a user id (uid) the arg() function returns the unaliased path.

thepaul’s picture

nevets, thanks for leading me in the right direction. The following snippet appears to be working for me.

<?php

// load the user object for the user in the profile
if(arg(0) == "user"){
  $thisuser = user_load(array('uid' => arg(1)));
  print theme('user_picture', $thisuser);
}
else{
  print theme('user_picture', $user->uid);
}
?>