0

I have a headless Drupal and I'm trying to figure out how to auto assign a role during registration based off of a custom field (field_is_student) in the registration form.

I am trying to use Rules but I don't know how to access the field to check whether it's true or false. My rule is set to "After saving a new user (rules_entity_insert:user)" and the only options I get in the condition Data Selector are User based.

Here's what I'm using to create my users:

{
    "name": {
        "value": "username"
    },
    "mail": { 
        "value": "useremail@email.com"
    },
    "field_is_student":{
        "value": false
    }
}
  • add a condition: user has a field, your student field, now that is available and you can compare it against whatever you need – pinueve Dec 27 '19 at 22:26
  • @pinueve Could you be more specific? The issue I'm having is there is no "User has a field" option or anything I can find that is like it. The only stuff that is exposed is username, created, email, language, etc – Brad O Dec 28 '19 at 0:46
  • @pinueve those are for Drupal 7 and completely useless to me. – Brad O Dec 28 '19 at 13:46
  • 1
    I also should mention that I provided a complete answer below, but it's basically what @pinueve said to do ... Also, all the concepts in Rules in D8 are almost identical to D7, so most of the old documentation is still quite relevant even though the UI looks different. It's certainly not useless ... – anonymous Dec 29 '19 at 2:33
0

The following Rule export does what you asked, for users created in the UI. In D8. I have tested it and it works. You will also need to use the (temporary) patch from https://www.drupal.org/project/rules/issues/2725525#comment-11438731 to work around the problem with in assigning roles.

The concept to understand is that Rules knows about the base fields for the entity type. But if you add fields, that's a new entity bundle, so you need to tell Rules that your data is of a certain bundle. To do that, you use the "Entity is of bundle" condition, as I show below. Then all the fields for that bundle will show up in the data selector.

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: 'Assign the ''student'' role to new users who identify themselves as students. Requires a boolean field ''field_is_student'' on the user object and requires an available role with machine id ''student''.'
tags: {  }
weight: 0
config_version: '3'
expression:
  id: rules_rule
  uuid: 36de9619-1401-4b21-a651-d3927c5b8dac
  weight: 0
  conditions:
    id: rules_and
    uuid: 416e90b0-d24c-4f37-8272-de0ecee5c375
    weight: 0
    conditions:
      -
        id: rules_condition
        uuid: bafe6fc4-f88f-4df2-af9b-2b3558257c2b
        weight: 0
        context_values:
          type: user
          bundle: user
        context_mapping:
          entity: user
        context_processors:
          type:
            rules_tokens: {  }
          bundle:
            rules_tokens: {  }
        provides_mapping: {  }
        condition_id: rules_entity_is_of_bundle
        negate: false
      -
        id: rules_condition
        uuid: 28787d22-54ca-49cf-af42-6f1011ed6494
        weight: 0
        context_values:
          operation: '=='
          value: '1'
        context_mapping:
          data: user.field_is_student.value
        context_processors:
          operation:
            rules_tokens: {  }
          value:
            rules_tokens: {  }
        provides_mapping: {  }
        condition_id: rules_data_comparison
        negate: false
  actions:
    id: rules_action_set
    uuid: 2933681a-a079-496d-bc79-f27ff53144eb
    weight: 0
    actions:
      -
        id: rules_action
        uuid: 38e8a3bb-0470-427f-80e1-53f6a703e8bf
        weight: 0
        context_values:
          roles:
            - student
        context_mapping:
          user: user
        context_processors:
          roles:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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