5

Is it possible in Drupal 8 to assign a specific role to each newly registered user?

  • Not from UI and out of the box. But assigning one specific role to all registered users doesn't make any sense since there is the 'authenticated' role. – user21641 Feb 4 '16 at 9:52
5

I had to implement a similar feature in a site, where:

  • Users are not required to verify their email address
  • When users create an account, they get the unverified account role
  • Once the users verify their email address, the unverified account role is removed, and they get a confirmed user role

It is pretty similar to what drupal.org does nowadays; I am just using different roles, and custom code that is different from what drupal.org uses.

I didn't use the Auto Assign Role module, since there isn't a working version for Drupal 8. I didn't use the Registration Role module, since it adds the roles to every account being edited, including the accounts with the administrator role.

I used a custom module (which I was already using for other purposes), where I implemented hook_ENTITY_TYPE_presave() with code similar to the following one.

function mymodule_user_presave(UserInterface $user) {
  if ($user->isNew() && !$user->hasRole('unverified account')) {
    $admin = FALSE;

    foreach ($user->getRoles(TRUE) as $id) {
      $role = Role::load($id);
      if ($role->isAdmin()) {
        $admin = TRUE;
        break;
      }
    }

    if (!$admin) {
      $user->addRole('unverified account');
    }
  }
}

hook_ENTITY_TYPE_insert() and hook_entity_insert() are the wrong hooks to use, since they are invoked when the entity is already saved in the database. Calling the save() method on the entity passed to those hooks will cause all the hooks invoked when an entity is saved to be invoked again, causing an infinite loop.

3

Assigning some role to newly registered users makes it possible to have features, functions, etc., that would be specific to NEWLY registered users. Like links to some "getting started" topics (e.g. in a block, or some menu links), which you don't want to show anymore to (eg) users that were registered X months ago, right?

IMO there are at least 2 ways of doing so, as further detailed below.

Auto Assign Role

This is what you could do using the Auto Assign Role module ... From its project page:

The Auto Assign Role serves three primary purposes. The first is to provide an automatic assignment of roles when a new account is created ...

This module is available for D6 and D7, for D8 you'll have to wait for issue #2220653, which includes a link to a sandbox project about it that you might want to look at ...

Rules

It's pretty forward to implement this functionality using the Rules module, at least in D7.

That would be my recommended approach. Because assuming you have rules installed for other modules, you don't need another module to be added, so it reduces the risk about modulitis (= way too much modules enabled)

Though for using Rules in D8 it's also too early (work in progress). But, modules with "many reported installs", often get more attention (and assigned resources) to be upgraded.

3

You could implement hook_ENTITY_TYPE_presave().

It will act before the entity is created, So you dont have to save the entity second time at your own risk, save will be manipulated by the core itself just add it in presave hook as mentioned.

function modulename_user_presave(Drupal\Core\Entity\EntityInterface $entity) {
 if($entity->isNew()) {
   $entity->addRole('rolename');
 }
}

You could also use the Registration Role module.

Registration role lets an administrator select a role to automatically assign to new users. The selected role will be assigned to new registrants.

1

You can use hook_ENTITY_TYPE_insert(), which is invoked after data is saved in the database.

function MY_MODULE_user_insert(Drupal\Core\Entity\EntityInterface $entity) {
  $entity->addRole('myrolestring'); //no rid!
  $entity->save();
}

You could probably also use hook_entity_presave() instead of hook_ENTITY_TYPE_insert(). I didn't test this, though.

  • Yes, this does work in "hook_entity_insert" as well – ben.hamelin Aug 3 '17 at 17:59

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.