Similar contents
Tag: Activating new accounts in Drupal automatically
Activating new accounts in Drupal automatically
Using Hook you can allow new accounts be activated automatically
Sometimes we want to enable news accounts automatically instead waiting for approval by adm.
Using the hook_auto_insert, you can allow the new accounts to get enabled, this hook is trigger at the insert of new account into the database and basically change the status of Block to Active.
Hook method
A simple and procedural way
OOP method - EventSubscriber
First I tried the AccountEvents::SET_USER, but I've noticed that this at this point the object account it was created and after a lot of tests I decide to change the approach.
So I've installed the contrib module: entity_events, this module allow to replacement for hook_entity_[update/insert/delete], it's really good :)
$ composer require 'drupal/entity_events'
Inside your custom module, path: root/src/EventSubscriber.
$user_access = $user_entity->get('access')->value;
// That's mean this insert is from user register.
}
On your root module path, create this file to allow the Drupal to know your new EventSubscriber
services:
custom_module.user_auto_enable:
class: Drupal\custom_module\EventSubscriber\CustomUserInsert
tags:
- { name: event_subscriber }
In some cases this hook is not enough, in my case, using Drupal 10, I also changed the user settings to visitors can register.
I used hook again, but this time the hook update to run only once, and it basically get the settings from database and add the visitors to allow register. This combination works pretty good.
I this case you should to update the database using the drush command: drush updb -y
www-data@3d7a062f7e96:/app$ drush updb -y
--------------- ----------- --------------- ----------------------------------------------------------------
Module Update ID Type Description
--------------- ----------- --------------- ----------------------------------------------------------------
custom_module 10001 hook_update_n 10001 - Allow the visitors to get auth without admin approval.
--------------- ----------- --------------- ----------------------------------------------------------------
// Do you wish to run the specified pending updates?: yes.
> [notice] Update started: custom_module_update_10001
> [notice] Update completed: custom_module_update_10001
[success] Finished performing updates.
That's all!
Join the conversation