Drupal 8: Crear un usuario programáticamente
30/11/2016 by Capy

Front.id

Cortito y conciso:

use Drupal\user\Entity\User;

$user = User::create();
$user->enforceIsNew();
$user->setEmail('mr.foo@gmail.com');
$user->setUsername('mr_foo');
$user->setPassword('foopass');
$user->activate();

$new_user = $user->save();

NOTA: A partir de Drupal 8.2 el email es opcional en los usuarios (Siempre que desmarques la opción "Require email verification when a visitor creates an account" en /admin/config/people/accounts )

Saludos!

 

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.