I'm new to Drupal 8 and not sure how I should make this work.

I have two users:

  1. User 1 is created by admin.
  2. User 2 is created by User 1 with predefined role.

I want to have different registration forms for admin and User 1.

So far, I've tried multiple registration modules, including the Registration Types module. The only thing I managed to do is assigning a role to User 2 when a checkbox is checked with the Select Registration Roles module, but it would be better if the roles are assigned automatically when User 1 creates User 2.

  • Did you read this question: How to assign a specific role to each newly registered user?? I like the hook (hook_user_insert) method in the second answer. – Cesar Moore Sep 3 '18 at 19:44
  • ... using the hook, you could check the role of the active user and then assign a different role to the newly created user. – Cesar Moore Sep 3 '18 at 19:51
  • drupal.org/project/registration_types seems to do exactly what you want. Where are you stuck? – leymannx Sep 3 '18 at 20:13
  • @leymannx Yes, I used regisration types, but the problem was when User 1 is logged in and tries to create another user it is denied access to the page. – Kellyy Sep 4 '18 at 6:30
  • @Kellyy It seems to me, that maybe you are using Drupal terms a bit off. When people talk about registration it is usually related to new users creating their own account. I think what you want, is to change the way existing users can create accounts for new users (not themselves). By default existing users can't use the register flow to create new accounts, but with admin permissions is't possible to create new user accounts, assign roles manually etc. Maybe what you need is a limited version of this feature for none admin, where parts are locked? – googletorp Sep 26 '18 at 12:38

Based on the 3rd answer on How to assign a specific role to each newly registered user?

I edited the code and tested this code:

 * Implements hook_ENTITY_TYPE_presave() for user entities.
function mymodule_user_presave(Drupal\Core\Entity\EntityInterface $entity) {
  $currentUser = Drupal::currentUser();
  if (in_array('administrator', $currentUser->getRoles())) {
  else {
|improve this answer|||||

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