Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By TWD on
I am using Profile2.
On the View Profile page I would like anyone with "view profile" permission to see what
role (or roles) that user has been assigned.
NOTE: This means the roles of the profile owner, NOT necessarily the currently logged in user. Some users have permission to view all profiles.
Further to that I DO NOT want to see the "Authenticated User" role included.
How to go about that?
I assume I need to edit one of the templates and include a PHP code snippet.
Any help most welcome.
Comments
When you find the template
When you find the template use:
global $user;
print_r($user->roles);
What you could do is
What you could do is providing a new variable called 'roleProfile' to variables available in template files.
I think a good way is to implement it in a custom module.
Please find in the following an example of a custom module.
Three files needed in a new folder called 'see_role_in_profile' in modules directory :
see_role_in_profile.info
see_role_in_profile.module
see_role_in_profile.tpl.php
Hope it helps
Benoit
Pure gold! Thank you very
Just to confirm something though, does this only return
the roles of the currently logged in user?
If so it's not quite what I wanted.
e.g.
I am user Jack Brown.
I have permission to view Bob Green's profile page.
I want to see what roles Bob Green has been assigned.
That's the correct functionality I am chasing.
Roles assigned displayed
I have just tested it, and the module actually shows the roles Bob Green has been assigned.
Benoit
* SOLVED !*
Finding the correct way, or even ANY way to do what I want was a needle in the haystack chase but I finally got it to work.
Step 1.
Add this to your template.php file in your current theme.
Step 2. Copy user-profile.tpl.php file from /mysite/modules/user into your current theme directory and add this to output the result.
<?php print render ($user_profile['roles']); ?>
Note that the code includes
implode(', ', array_slice($account->roles, 1));
since "roles" is by nature an array AND the first item of that array should be "authenticated user" so we use the argument "1" to offset the first item in the array. What is left should be all the roles assigned to the user who owns the profile page you are viewing, represented as a comma separated string. Mileage may vary depending on your needs.
http://us2.php.net/manual/en/function.array-slice.php
It is possible to do something similar with email accounts if you want to show the user email account on the profile page.
In template.php
and then add then following to user-profile.tpl.php
<?php print render ($user_profile['mail']); ?>
Credit to Sagar Ramgade for this post comment:
http://drupal.org/node/1161236#comment-6163514
IMPORTANT !!!
I forget where I read it but somebody mentioned they were having
problems getting new variables to show up on the user-profile.tpl.php
page when the Display Suite Module was enabled.
I had that situation so I needed to disable Display Suite.
Perhaps somebody knows the reason or has a workaround?
Thanks for feedback !
Hi Twd,
Nice solution, thank you for your feedback.
Having the code in a module or in a theme depends of what we want...
Using field_attach_preprocess is a great solution !
I will try to test it with Display Suite Module enabled.
Perhaps this module alters the way user_profile array is built.
Benoit
Display suite case
I've just test it with Display Suite.
DS module provides new layouts to display fields in user display settings (in admin/config/people/accounts/display).
This layouts are template files.
Don't know if it is the real problem, but Drupal uses templates to display values, if DS module uses its own templates our custom template called user-profile.tpl.php will never be used anymore.
Hence the variable $user_profile['roles'] will never be displayed.
Benoit
Thanks for all your help and
Thanks for all your help and feedback.
Hi,
Hi,
just Edit the template file : user-profile.tpl.php
then use :
print end($elements['#account']->roles;
I use it in my web site, and it works fine.
awesome
Yeah! It works for me. Thank you
In case anyone is wondering
In case anyone is wondering how to do this on Drupal 7, here's a How-To. I'm using a custom Zen theme from STARTERKIT. This should work for most themes.
1. Copy modules/user/user-profile.tpl.php to your theme's templates folder.
2. Paste the following snippet above the HTML render code:
3. The $rid > 3 statement will print any role after Admin (anonymous = 1, authenticated = 2, admin = 3, all the others are 4+). Create whatever conditions you need to filter the roles.
.
Thanks for the D7 solution -
Theme - Corporateclean - does not work. I need to delete/rename the original file under the core module of modules/user/user-profile.tpl.php.
Update - well, it turns out that deleting the user-profile.tpl.php file it as described above from the core module folder did the trick. After you can add it again. This should have worked with cache clearing I guess, but this is how it worked finally.