How to create a user account programmatically in Drupal 8 ?
Question
Here an example for create a user account programmatically in Drupal 8
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user = \Drupal\user\Entity\User::create();
//Mandatory settings
$user->setPassword('password');
$user->enforceIsNew();
$user->setEmail('email');
$user->setUsername('user_name');//This username must be unique and accept only a-Z,0-9, - _ @ .
//Optional settings
$user->set("init", 'email');
$user->set("langcode", $language);
$user->set("preferred_langcode", $language);
$user->set("preferred_admin_langcode", $language);
//$user->set("setting_name", 'setting_value');
$user->activate();
//Save user
$res = $user->save();
Comments13
How to get uid of created user?
How to get uid of created user?
After $user->save();…
After $user->save();
$uid = $user->id();
In which file should I…
In which file should I insert this code?
Please follow this tutorial…
Please follow this tutorial to create a page then you can create using this code
Create a new module http://drupal8.ovh/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page
In which file should I…
In which file should I create new users ??
Please follow this tutorial…
Please follow this tutorial to create a page then you can create using this code
Create a new module http://drupal8.ovh/en/tutoriels/5/create-a-simple-module-for-drupal-8-step-2-create-simple-page
Worked like a charm. :)
Worked like a charm. :)
Not working for me
I don't wanna write a module and I be able to use drupal functions in my custom php script by including the kernel and bootstap.inc. But when I import $user = \Drupal\user\Entity\User::create(); it returns 500. Any idea ..?
You can't use it without drupal.
You can't use it without drupal, the example is usable only in a drupal 8 module.
create user account programmatically drupal 8
Is possible to create custom module for admin area? In this case, we want to sync a lot of accounts from another system/web app to our drupal site so we don't need to create manually using "people menu" in admin area.
Create Account User
How to exites user Already Register
Activation
How to send a activation mail?
please uploads full source…
please uploads full source code