Problem/Motivation

I want to auto-assign roles to users depending on the domain of the email address they use to register

I had the impression from the description that RR should be able to do this, but on installing it I found no way to make it conditional. Is this possible? If not, could you please suggest some other module?

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

adam_b created an issue. See original summary.

darkdim’s picture

Assigned: Unassigned » darkdim

darkdim’s picture

Component: Documentation » Code
Assigned: darkdim » Unassigned
Status: Active » Needs review

darkdim changed the visibility of the branch 3380038-how-to-conditionally to hidden.

darkdim’s picture

mlncn’s picture

Version: 2.0.0 » 2.x-dev
Category: Support request » Feature request

Oooh very interesting. Could you describe how the event can be intercepted/used?

darkdim’s picture

Yes, sure.
1. Create a module
2. You need to describe the service as an event subscriber
3. Create an EventSubscriber directory in the src folder
4. Create the subscriber class itself

There is a book with a code, at the address you can see events https://selwynpolit.github.io/d9book/events

services:
  my_module.roles_subscriber:
    class: Drupal\my_module\EventSubscriber\ClassSubscriber
    arguments: []
    tags:
      - { name: event_subscriber }
<?php

namespace Drupal\my_module\EventSubscriber;

use Drupal\registration_role\Event\SelectRoleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ClassSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    $events[SelectRoleEvent::SELECT_ROLE_EVENT][] = ['registrationRole', 0];
    return $events;
  }

  public function registrationRole(SelectRoleEvent $event) {
    $user = $event->getUser();
    ... logic ...
  }

}

mandclu’s picture

It's worth pointing out that you could probably also handle this in configuration by using ECA.

mlncn’s picture

@darkdim rather than having the overhead (and harder-to-follow code) of actually implementing the the event handler, is it OK to treat it as an "alter" event and only have an example of implementing it, perhaps in a registration_role.api.php file?

Thanks so much, i do think we should bring this in before we change anything that would break applying this merge request as a patch.

Current code in: https://git.drupalcode.org/project/registration_role/-/merge_requests/3/...

Also is the gitlab-ci.yml something all contrib projects should have now?

anybody’s picture

@mlncn so should this be closed now?