I have written a small php snippet to get the name of the user role showing on a user MyAccount page.

<?php
global $user;
if (in_array('senior member',$user->roles)) {
echo "Senior Member";
}
else if (in_array('full member',$user->roles)) {
echo "Full Member";
}
else if (in_array('authenticated user',$user->roles)) {
echo "New Member";
}
else {
echo "Logged Out";
}
?>

I imagine my php is horrid (php noob!)...

Now the problem is...it shows the viewer's role, not the role of the member's page. How do I need to alter the code to show the role name of the user whose MyAccount page it is, not the person who's viewing the page?

I tried $node->user->role (something similar works for nodeprofile) but without luck.

Comments

ThatPerson’s picture

The variable $user is a variable that handles the current user, not any other user.

You'd probably want to use a function like user_load (http://api.drupal.org/api/5/function/user_load) to load the user object and use that. The code could look something like:

Disclaimer: I'm not an expert in Drupal programming (I haven't done any, really) so if you can find a function/s that better suits the task, do it.

$userpage = user_load(array('uid' => arg(1))); /* There's probably a better function to get the UID of a user other than using arg() */
if (in_array('senior member',$userpage->roles)) {
echo "Senior Member";
}
else if (in_array('full member',$userpage->roles)) {
echo "Full Member";
}
else if (in_array('authenticated user',$userpage->roles)) {
echo "New Member";
}
else {
echo "Logged Out";
}

Try that. And find a better function to get the user ID other than the arg() function. And don't use the variable $userpage; use something more informative (your choice).

esllou’s picture

it shows everyone as a new member, even though some of the ones I checked are certainly senior and full members.

:-s

what could be the problem?

nevets’s picture

For example, if you are overriding the theme function $account should work. The question is what is the context for the snippet, how does it's output end up on the myAccount page?

esllou’s picture

I am adding this code directly to a user_profile.tpl.php page.

esllou’s picture

wow...all I did was to take out the line:

global $user;

and it's working perfectly, showing me the role name of the user whose MyAccount page I'm viewing instead of mine.