Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
This prints "Object" for the user (not correct) and prints nothing for the role. Yet all the messages indicate the way to get role is $user->role.
Thanks for any ideas.
I did print_r($user) and I got all of the details about the user. There wasn't a role value but there was a roles value. After some tinkering, I found this to work:
i f ($user->roles[2]) // I know [2] corresponds to the admin role
do Admin stuff
if ($user->roles[3]) // I know [3] corresponds to the designer role
do Designer stuff
Comments
$user object
Use $user->rid for the role ID, or $user->role for the role name. Be sure to declare $user as global.
Use $user->roles instead
Hi, I'm using Drupal 4.5.2 with PHPTemplate and $user->role did not work for me.
I have users with multiples roles and used something like this to see who could edit some node ("administrador" is a role that I created):
Alexis Bellido - Ventanazul web solutions
that fixed it
Thanks so much for your help!
Multiple roles
Yes, drupal has added support for multiple user roles since the original post was made.
--
If you have a problem, please search before posting a question.
not working here
I am trying this same thing in a block, and using the example code. The $role is not ever getting filled. My code:
neither variable is right
I'm seeing the same issue:
global $user;
global $r = $user->role;
print $user;
print $r;
This prints "Object" for the user (not correct) and prints nothing for the role. Yet all the messages indicate the way to get role is $user->role.
Thanks for any ideas.
print_r()
global $user;
print_r($user);
this will get you farther than print()....
$user->roles[X] works
OK, thanks. Sorry, I'm new at this.
I did print_r($user) and I got all of the details about the user. There wasn't a role value but there was a roles value. After some tinkering, I found this to work:
i f ($user->roles[2]) // I know [2] corresponds to the admin role
do Admin stuff
if ($user->roles[3]) // I know [3] corresponds to the designer role
do Designer stuff
Thanks again.
In_array may be more helpful
since roles is an array you might find this a more helpful way to use it.
of course administrator may be replaced by any role.
hope that helps
Randy
How to use this in a theme?
answered my own question
this page is pretty darn useful for themers
http://drupal.org/node/45774