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 use:

global $user;
print_r($user->roles);

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 :

  1. see_role_in_profile.info
  2. see_role_in_profile.module
  3. see_role_in_profile.tpl.php

see_role_in_profile.info

name = See Role in Profile Page
description = "Anyone with the view profile permission can see what role(s) that user is assigned.
Except Authenticated role"
core = 7.x
dependencies[] = profile2

see_role_in_profile.module

<?php
/**
* @file
* See Role in Profile Page
* @author drupal.org/user/1453788
*/
/**
* Implements hook_theme_registry_alter().
*/
function see_role_in_profile_theme_registry_alter(&$theme_registry) {
 
$theme_registry['profile2']['theme path']  =   drupal_get_path('module', 'see_role_in_profile');
 
$theme_registry['profile2']['template']  =   drupal_get_path('module', 'see_role_in_profile').'/see_role_in_profile';
}
/**
* Implements hook_permission().
*/
function see_role_in_profile_permission() {
 
$permissions = array(
   
'see role in profile' =>  array(
     
'title' => t('See Role in Profile Page'),
     
'description' => t('Anyone with the view profile permission can see what role(s) that user is assigned.')
      )
    );
  return
$permissions;
}
/**
* Implements hook_preprocess().
*/
function see_role_in_profile_preprocess(&$variables, $hook) {
    if (
user_access('see role in profile')) {
        if (
$hook=='entity' && $variables['elements']['#entity_type']=='profile2') {
           
$userSeen = user_load($variables['user']->uid);
            unset(
$userSeen->roles[2]);
           
$variables['roleProfile'] = t('My role(s) :!roles ',array('!roles' => implode(', ', $userSeen->roles)));
        }
    }
}
?>

see_role_in_profile.tpl.php

  • Copy Paste from sites/all/modules/profile2/profile2.tpl.php
  • Add the following code

    <?php if ($roleProfile):?>
    <div style="color:red"><?php print $roleProfile ?></div>
    <?php endif?>

Hope it helps

Benoit

--------------------------
Share & Bon Marché

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.

I have just tested it, and the module actually shows the roles Bob Green has been assigned.

Benoit

--------------------------
Share & Bon Marché

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.

/**
* Process variables for user-profile.tpl.php.
*
* The $variables array contains the following arguments:
* - $account
*
* @see user-profile.tpl.php
*/
function YOUR_THEMENAME_preprocess_user_profile(&$variables) {
  $account = $variables['elements']['#account'];
  // Helpful $user_profile variable for templates.
  foreach (element_children($variables['elements']) as $key) {
    $variables['user_profile'][$key] = $variables['elements'][$key];
  }
  //Add roles to $user_profile variable
   $variables['user_profile']['roles'] = implode(', ', array_slice($account->roles, 1));
  // Preprocess fields.
  field_attach_preprocess('user', $account, $variables['elements'], $variables);
}

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

/**
* Process variables for user-profile.tpl.php.
*
* The $variables array contains the following arguments:
* - $account
*
* @see user-profile.tpl.php
*/
function YOUR_THEMENAME_preprocess_user_profile(&$variables) {
  $account = $variables['elements']['#account'];
  // Helpful $user_profile variable for templates.
  foreach (element_children($variables['elements']) as $key) {
    $variables['user_profile'][$key] = $variables['elements'][$key];
  }
  //Add mail to $user_profile variable
  $variables['user_profile']['mail'] = $account->mail;
  // Preprocess fields.
  field_attach_preprocess('user', $account, $variables['elements'], $variables);
}

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?

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

--------------------------
Share & Bon Marché

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

--------------------------
Share & Bon Marché

Thanks for all your help and feedback.

Hi,

just Edit the template file : user-profile.tpl.php

then use :

<?php
print end($elements['#account']->roles;
?>

I use it in my web site, and it works fine.

_______________________________________________
My best website :

RLSGAME.ORG

Take a look