2

I have Drupal 7 installed on my local WAMP server.

I need some simple solution to let users choose one role from 2 offered on their account page AFTER registration.

Is it possible to do it with the Rules module? - when a user follows one of the 2 links he is assigned the corresponding role. The problem is I cannot find the proper Rules Event that could initiate this process.

As the last resort I would probably have to use the Auto Assign Role module.

2 Answers 2

1

Using Rules, I would do it this way (untested):

  • Create a user field, select list (or radio button, whichever you prefer) - we will call this field_user_role_choice with the options (Role1Name, Role2Name)

First Rule:

  • Create a new Rule, React on Event: After updating an existing user account

If you don't assign a default value,

  • Condition: Data value is empty - selector: account:field_user_role_choice, Negate (this is to ensure it doesn't trigger on a registration or if the user doesn't select a role)

  • Condition: Data comparison - selector: account:field_user_role_choice, value: Role1Name key

  • Actions: Add User Role - whichever role is associated with Role1Name

  • Actions: Remove a User Role - whichever role is associated with Role2Name (this is to prevent both roles being assigned to one person)

Second Rule:

  • Create a new Rule, React on Event: After updating an existing user account

If you don't assign a default value,

  • Condition: Data value is empty - selector: account:field_user_role_choice, Negate (this is to ensure it doesn't trigger on a registration or if the user doesn't select a role)

  • Condition: Data comparison - selector: account:field_user_role_choice, value: Role2Name key

  • Actions: Add User Role - whichever role is associated with Role2Name

  • Actions: Remove a User Role - whichever role is associated with Role1Name (this is to prevent both roles being assigned to one person)
6
  • Thanks a lot Geoff! It works like charm. The only thing is that I would not like to allow users to change their role once they selected it. We could add another condition that account does not have role1Name OR role2Name (user has roles/negate). But this can confuse user as when they change the role again nothing happens although the select list is active. Is there a way to disable this field or at least hide it with shown status message "You have the role of ..." to respect usability?
    – Alex777
    Commented Apr 12, 2015 at 17:52
  • Trying to keep it simple, and keep it 'in the family', you could try Rules Forms Support (drupal.org/project/rules_forms) it integrates form triggers into rules - I don't know for sure what actions can be done, but I believe items can be hidden. So a third rule would check to see if a value is set in that field, if so, hide the element preventing the user from changing it
    – Geoff
    Commented Apr 13, 2015 at 0:32
  • Oh, and you may want to add a condition about the role of the person, that they not be admin, or you may be unable to change a user's role manually later on if necessary
    – Geoff
    Commented Apr 13, 2015 at 0:37
  • Hi, sorry for keeping silent for a while - I tested various options. The module you mentioned indeed was designed to disable the field. However I realised that doing the whole thing with Rules will not work in proper manner. I will explain: the newly created user field placed on the user account page is not related to users_role table in any way. If the Admin changes the role of this user from Role1 to Role2 (from admin interface, not from user's page) the user will see on his account page that he still has Role1, although in reality he will have permissions corresponding to Role2.
    – Alex777
    Commented Apr 14, 2015 at 20:22
  • to finish: I think the proper way is to add Role form widget in a custom module. If you could give me a hint abt functions to use I would aappreciate. thanks
    – Alex777
    Commented Apr 14, 2015 at 20:37
0

This is what you need Auto Assign Role

The second is to allow the end user the option of choosing their own role or roles when they create their account

1
  • I have retested now, and alas! - Auto Assign Role can do this ONLY DURING registration. i need it to be done AFTER registration due to some specific needs. User has signed up and then he can choose wich role to take. So this module is no good for this.
    – Alex777
    Commented Apr 12, 2015 at 12:30

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.