I have a special role (role A) that I have to have set with view user profiles permission. I then have another role (role B) as well as the administrator role that I don't want for that roles users to show up in a views exposed filter and/or for the users with role A to be able to view their profiles. Is there:

a. a way to stop users from certain roles showing up in the exposed filters.

b. a generic way (or module) that stops certain roles being able to view other certain roles.


I solved b with this module which works well:


So just need to come up with a way of preventing certain roles from showing in a views exposed filter

2 Answers 2


For A.

Set up another filter in your view for "User: Roles". Choose the Operator "Is none of" and then, choose in the options the role you dont want seen. You can use the roles filter after the exposed one.

Attached is a pic of the user:roles filter section. The options are cutoff to the right (taken from a working site..want to leave the role names private)

enter image description here

You can also expose the role filter and, limit it to the selections you choose in the options pane.

  • This is great if you want to stop the roles showing in the view result, it does not however stop the users from the roles showing in an exposed filter.
    – 7wonders
    Sep 5, 2012 at 12:35
  • If you expose the role filter then, you can limit the roles that show up in the filter. There is a check box below the options for "Limit list to selected items" This is in drupal 7, Views 3. Sep 5, 2012 at 16:11
  • I know, but it doesn’t stop the users from those roles showing up in the other exposed filters.
    – 7wonders
    Sep 5, 2012 at 17:48
  • updated the question to reflect the problem better
    – 7wonders
    Sep 5, 2012 at 17:50

Thanks to googletorp's answer here, I have figured it out. If anyone has a better way of doing it then please add. Basically you do a form_alter on the exposed filter:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_exposed_form') {
    $form['field_my_field_target_id']['#options'] = mymodule_options_filtered_by_roles();

function mymodule_options_filtered_by_roles() {
  $role_users = db_select('users_roles', 'ur')
    ->fields('ur', array('uid'))
    ->condition('rid', array(3, 6)) //Here I add the role id's I dont want to show

  foreach ($role_users as $ru) {
    $role_uids[] = $ru->uid;

  $users = db_select('users', 'u')
    ->fields('u', array('uid','name'))
    ->condition('uid', $role_uids, 'NOT IN') // get all the uids of users not in the above roles
    ->condition('uid', 0, '!=') // get rid of uid 0

  $user_array = array('All' => t('- Any -')); //Start the array with all
  foreach ($users as $user) {
    $uid = $user->uid;
    $name = $user->name;
    $user_array[$uid] = $name;
  return $user_array;

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.