0

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
1

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.

Thanks

  • Yes, The Rule method will work – Alen Simonyan Jun 24 at 11:06
0

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.'
events:
  -
    event_name: 'rules_entity_insert:user'
description: ''
tags: {  }
config_version: '3'
expression:
  id: rules_rule
  uuid: 36de9619-1401-4b21-a651-d3927c5b8dac
  conditions:
    id: rules_and
    uuid: 416e90b0-d24c-4f37-8272-de0ecee5c375
    conditions: {  }
  actions:
    id: rules_action_set
    uuid: 2933681a-a079-496d-bc79-f27ff53144eb
    actions:
      -
        id: rules_action
        uuid: 38e8a3bb-0470-427f-80e1-53f6a703e8bf
        context_values:
          roles:
            - granted_by_rules
        context_mapping:
          user: user
        context_processors:
          roles:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add

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