Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Create a user account programmatically in Drupal 8

By editor , 14 November, 2015
Question

How to create a user account programmatically in Drupal 8 ?

    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();

 

Create a new module

Tags

  • Drupal 8
  • Code
  • User
  • Add new comment

Comments13

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Rijin (not verified)

10 years 11 months ago

How to get uid of created user?

How to get uid of created user?

  • Reply

editor

10 years 6 months ago

In reply to How to get uid of created user? by Rijin (not verified)

After $user->save();…

After $user->save();

$uid = $user->id();

 

  • Reply

badminton (not verified)

10 years 8 months ago

In which file should I…

In which file should I insert this code?

  • Reply

editor

10 years 6 months ago

In reply to In which file should I… by badminton (not verified)

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

 

  • Reply

Anonymous (not verified)

10 years 8 months ago

In which file should I…

In which file should I create new users ??

  • Reply

editor

10 years 6 months ago

In reply to In which file should I… by Anonymous (not verified)

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

  • Reply

Anonymous (not verified)

10 years 7 months ago

Worked like a charm. :)

Worked like a charm. :)

  • Reply

Eyosiyas Tadele (not verified)

9 years 5 months ago

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 ..?

  • Reply

Net (not verified)

9 years 5 months ago

In reply to Not working for me by Eyosiyas Tadele (not verified)

You can't use it without drupal.

You can't use it without drupal, the example is usable only in a drupal 8 module.

  • Reply

ugy (not verified)

9 years 4 months ago

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.  

  • Reply

Radhey (not verified)

8 years 10 months ago

Create Account User

How to exites user Already Register

  • Reply

Kachtouli (not verified)

8 years 3 months ago

Activation

How to send a activation mail?

  • Reply

Anonymous (not verified)

4 years 8 months ago

please uploads full source…

please uploads full source code

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer