You are here

Modify (add or remove user roles) the user roles of lots of users using Bulk Operations and Views. In Drupal 7

Scenario- A request came in that I develop a newsletter for users that choose a certain profession in their user account profile. IE: All Physicians would be added to a role created named "Physicians" and only users with the permission role "Physicians" would be allow to view certain content on the site and receive the "Physicians Only" newsletter. We had about 1000 or so existing users in the database that currently held the job profession as "Physician". Sooooooo I needed to assign all those existing users the role "Physician". Here are the steps...

 

First of all you need to install the Views and Bulk Operations modules.

Here are the links for easy reference-
https://www.drupal.org/project/views_bulk_operations
https://www.drupal.org/project/views

 

1. Create a view by going to Structure>Views> Add new view

2. Name your new view and give it an easy URL and Your view should have:
show: users
create a page display
format: Table of Fields
Items to display: 100 (I did 100 because I had so many but you can put want you want here)
pager: checked
Then click continue & edit

3.add Fields user: Name
Bulk operations : user
***In the Bulk Operation you want to change Selected Bulk Operations to "Modify user roles"

4. Add filters
So since I needed all users that were physicians but not assigned the "Physician" role yet... My filters were the following:
User: Active (dahhh)
User:Current Position (Physician)
User: Roles (<> Physician) ***that means is not assigned the "Physician" role.

5. Click save and go to the views page you just created and YEAY! You will be presented with a table of all the users you need to modify and you can use the bulk operation at the top to modify them all at once! :)