I'm using the Drupal 8 version of the Rules module. How do I add a role to new users with a rule?

I created a rule.

  • Name: New user role
  • Events: After saving new user
  • Action: Add user role
  • Data selector: user.roles

I don't know what to set for value. Is my rule correct?

I want to add the particulier role to users.

  • Did you check this drupal.org/node/2815967 – Pankaj Yogi Jul 28 '17 at 13:27
  • @PankajYogi Thanks, i will uninstall rules because i can not use it on drupal 8 – ZenImagine Jul 28 '17 at 13:32
  • I suggest you use EventListeners. You also could solve this by using the EntityCreate hook! – ssibal Jul 28 '17 at 13:38

I also have tested rules module in drupal8 but data selectors did not worked, as also mentioned here https://groups.drupal.org/node/516054

Then i tried https://www.drupal.org/project/registration_role which worked for me. You may also try this module if you want to assign a role to a user on registration.


| improve this answer | |
  • Yes, The Rule method will work – Alen Simonyan Jun 24 '19 at 11:06

This works to set a role for a new user. This rule reacts on a new user being created, and assigns that new user a role with machine name "granted_by_rules". You can import this onto your site and see how it's configured.

langcode: en
status: true
dependencies: {  }
id: set_a_role_for_new_users
label: 'Set a role for new users.'
    event_name: 'rules_entity_insert:user'
description: ''
tags: {  }
config_version: '3'
  id: rules_rule
  uuid: 36de9619-1401-4b21-a651-d3927c5b8dac
    id: rules_and
    uuid: 416e90b0-d24c-4f37-8272-de0ecee5c375
    conditions: {  }
    id: rules_action_set
    uuid: 2933681a-a079-496d-bc79-f27ff53144eb
        id: rules_action
        uuid: 38e8a3bb-0470-427f-80e1-53f6a703e8bf
            - granted_by_rules
          user: user
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add
| improve this answer | |

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