Is it possible to have the result of a views user list only contain users with a specific role that the current user also has?

I'm not familiar with Views, but it seems to fit the bill for making a better looking and more usable user list than the one that comes standard with Drupal. However, I need to have the user list filtered.

I setup multiple user groups that are identified by a specific role (like groupmember - group A). Admin users of that group have an additional admin role (like group manager), but they should only see users in their own group when using the view.

Can something like this be done? I'm writing modules so I could use the api if that is the way to go. I'm using Drupal 7.

2 Answers 2


As I understand, you want to create a view which list users which lists only those users who has a specific role or roles that the current user also has. The below approach can be a solution :

  1. Add a field 'User: Uid' in your view (Let the view is of type 'Users').

  2. Make the field as 'Exclude from display'.

  3. Add a 'global php' field and add some code as below under 'VALUE CODE' :

    $user_load = user_load($data->uid);
    $data->user_role = $user_load->roles;
    return ($data->user_role);

    Here, we are getting the roles of each user being rendered.

    Here used '$data->uid' instead of '$row->uid' to avoide issue of returning NID with $row in D7. Although in this case '$row->uid' returns correct information.

  4. Make the field as 'Exclude from display' as well.

  5. Under Filter Criteria add a 'global php' field and add the below codes :

    i) Under 'Setup Code' :

     global $user;
     $roles = array();
     $view->roles = $user->roles;

    Here we are getting all the roles of the current user.

    ii) User 'Filter Code' :

     // Suppose you have five roles in the instance with the 'rid's as saved in
     // 'role' table as : 1 (anonymous user), 2(authenticated user), 3(administrator), 4(hr), 5(web admin)
     // Suppose ROLE you want be matched has RID = 4
     if (isset($view->roles[4])) {// Checking if the current user has the role that you want to be matched with
       if (!isset($data->user_role[4])) {// If the user getting rendered doesn't have that role
         return TRUE;// If TRUE is returned, then that row is NOT shown

Note : Considering, 'views_php' module is installed.

Note : If the current user doesn't have the role that you want be filtered for, then the view will list the default list of users based on other conditions.

Note : Instead of writing the 'rid' in the filter code directly, we can provide an admin menu which will render a custom form with a text field and, admin or user with required access permission, can set any role(s) through that form and save it in a drupal variable, and we can render the role information we want be matched from the variable inside our filter code.

Hope it helps

  • Awesome, that exactly pointed out how to manipulate the filters. I needed something a bit different, but you showed me the way! Thanks. Two remarks: could you update your post and mention you need the Views PHP module for this? And could you also mention at 5ii that if TRUE is returned, then that row is NOT shown? Maybe saves someone else an hour of feeling stupid ;-)
    – Whiskey
    Commented Jul 24, 2013 at 21:51

In Views, you can add a block showing those users which have the same roles as current user has:

An example of view block configuration:

The current user has to be autencitaded, so we'll be able to get his roles:

BLOCK SETTINGS -> Access: Role | authenticated user

Now, add contextual filters:

User: Roles

When the filter value is NOT available -> Provide default value -> PHP code:

global $user;
if ($user->uid) {
  $roles = $user->roles;

  // unset 'authenticated user' ( related users doesn't have to be logged in )
  $roles = array_diff($roles, array('authenticated user'));

  // get roles keys
  $roles = array_keys($roles);

  // compose the arguments
  // in order to displaying only users with exactly the same roles as current user has
  // the arguments will be separated by commas.
  $arguments = implode(",", $roles);
  return $arguments;
else {
  return false;

Users might have multiple roles, so multiple values have to be allowed:

More -> check: Allow multiple values

That's all for this contextual filter. Save it.

Probably, you would need to remove the current user from the view, because he don't need his name on the list. To do that, add next contextual filter:

User: Uid

When the filter value is NOT available -> Provide default value -> User ID from logged in user

MORE -> check: exclude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.