Community Documentation

Tutorial: Add role to user based on a user field during registration

Last updated December 22, 2012. Created by WorldFallz on December 21, 2012.
Log in to edit this page.

Automating the addition and removal of roles to users based on site events is one of those very common tasks that seem unnecessarily difficult with Drupal. The good news is, it's far more difficult to figure out than it is to do!

Previously, there have been several contributed modules aimed at various incarnations of this functionality (Auto Assign Role being one of, if not the, most popular). However, with D7 and Rules2 many of these singular function contributed modules seem to have fallen by the wayside. Rules is far more flexible, but also more difficult to master. If a contributed module does what you need then great, but if you already have rules installed (and many sites do) or need the functionality for a use case not handled by or slightly different than the contributed module, then rules is the way go.

Modules Required

Add the Field

  1. Install the role_field module like any other module.
  2. Navigate to admin/config/people/accounts/fields.
  3. Add a new field for "User Role" (or whatever you want to call it) and select the "Role" field type.
  4. Configure the field as desired. In particular, it makes sense to exclude the anonymous, authenticated, and administrator roles. The first 2 are handled by Drupal automatically, and the administrator role is not likely one you want users granting themselves!

Configure the Rule

  1. Navigate to admin/config/workflow/rules.
  2. Click "Add new rule".
  3. Enter a name for the rule (i.e. "Assign User Role").
  4. Select "After saving a new user account". Then, in order to react to both events and keep the role in sync with the user field, add a second event for "After updating an existing user account".***
  5. Click "Add condition".
  6. Select "Entity has field".
  7. Set the Data selector to "account" to act on the registering user.
  8. Select the Role field created above.
  9. Save the condition.
  10. Click "Add action".
  11. This is where it gets very confusing. One would think the action should be "Add user role" which turns out to be incorrect. To add a role to existing roles, select the "Add an item to a list" action. To replace a role, set a specific role, or set the only role a user has, select the "Set a data value" action. Since this tutorial is about adding a role during registration, and will therefore be the only role the user has, select the "Set a data value" action.
  12. Set the Data selector for the data to "account:roles:0" (you can expand the Data Selectors drop down to see the other available options).
  13. Click "Continue".
  14. Set the Data selector for the item to add to "account:field-user-field". Be sure substitute the name of your field for "user-field". If you're not sure of the exact selector syntax, expand the "Data Selectors" drop down and find it in the list.
  15. Click "Continue".
  16. In the "Value" fieldset, click the "Switch to data selection" button.
  17. Set the Data selector for the Value to "account:field-user-role". Again, if you're not sure of the exact selector syntax expand the "Data Selectors" drop down and find it in the list.
  18. Click "Save".

***If you only want this role set on user registration, then you can use the Field Permissions module to prevent modification of the field after registration.

Rules Export

{ "rules_assign_user_role" : {
    "LABEL" : "Assign User Role",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules" ],
    "ON" : [ "user_insert", "user_update" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "account" ], "field" : "field_user_role" } }
    ],
    "DO" : [
      { "data_set" : {
          "data" : [ "account:roles:0" ],
          "value" : [ "account:field-user-role" ]
        }
      }
    ]
  }
}

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Level
Beginner
Audience
Site builders
Keywords
roles, Rules

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.