Is their a way to display users role in my account page?

Thanks in Advance
Lu

Comments

mokargas’s picture

Yes. It involves editing the user template files.
You'll probably want to read this on editing user profiles : http://drupal.org/node/35728

Some (untested but basic) example code to display roles:

global $user ;

foreach ($user->roles as $role) {
    print  $role;
}

hackia’s picture

I need to be spoon feed this i am not that great at this stuff, can someone tell me where to place the code and what the code exactly is.

Thanks

excellent_new’s picture

hi

global $user ;
$role_array = $user->roles;
//it print the all role from the role table
       foreach ($role_array as $role)
       {
	   	     echo $role;// role from the role table which created
                    if($role='jobseeker')
                    {
                             echo "jobseeker user"; //it print this if user is the jobseeker
                     }

       }

means this code u can put in any place of your page/node
but actually what u want to do,means u want to display all role ya check the current user's role

Tulsa

hackia’s picture

It Worked.

I just made a user-profile.tpl.php file in my theme folder, copied the original user-profile content and pasted your code and it showed the user role, but i wish i can make it only show the last role, it shows "authenticated userStudents" Students being the second role

Got any clue of how to make it only show the last role in place?

Thanks, man your code worked.

excellent_new’s picture

global $user ;
$role_array = $user->roles;
//it print the all role from the role table
       foreach ($role_array as $role)
       {
      
                    if($role='userStudents')
                    {
                             echo $role; //it  print  Students role only
                     }

       }
        echo $role // it prints the last role of role table

your last role is fixed so u can echo the $role variable after the forech loop

may be it helps

Tulsa

Tergenev’s picture

The above bits of code all have one problem. They show the *viewing* user's roles on ALL users' profile pages. To see the roles of the profile you are viewing, whether it is the currently logged in user or not, you just change the role array assignment from $role_array = $user->roles; to $role_array = $account->roles;. I did a slightly more direct coding than the above, but here's what worked for me:

<?php
print '<hr /><br><strong>Type of Account:</strong><br>';
foreach ($account->roles as $role) {
    if($role != 'authenticated user') {
	print  ("\"$role\" ");
	}
}
    print '<hr />';
?>

Which looks like this on my profile page.