4

I've been searching for this for ages, but could not come with a solution. Drupal 7 provided a way to get the user ID of a newly created user after running user_save(); the new user has been added and the user ID has been returned in the object.

In Drupal 8, when I use User::create(), it only returns a constant based on whether the user has been created or updated. Is there any way to get the user ID after the user has been created?

3 Answers 3

6

I use the following code in a custom entity method.

$storage = $this->entityTypeManager()->getStorage('user');
$owner =  $storage->create(['mail' => $email, 'name' => $this->getName()]);
$owner->save();
$uid = $owner->id();

If you don't have access to entityTypeManager() in your class, you could alternatively use the following code.

$storage = \Drupal::service('entity_type.manager')->getStorage('user');

When possible, use dependency injection instead of calling a \Drupal method.

3
  • 4
    Yes, this is the right code to get the id. But the problem is not User::create(), it's that you need to save before you can get the id.
    – 4uk4
    May 11, 2017 at 21:56
  • Well, I do call the save method after calling the User::create(), i.e. User::create()->save() and that returns the SAVED_NEW or SAVED_UPDATED constant based on what action is performed. It's written in the method comments.
    – crzpiot
    May 12, 2017 at 5:10
  • 3
    The problem was that the methods cannot be chained. You have to call User::create() and then I was assigning the result of the save() method to the same variable that was previously populated with the new entity object. If you do not chain the methods, both mine and the above specified methods work. Thanks a lot.
    – crzpiot
    May 12, 2017 at 5:20
1

In Drupal 8, when I use User::create(), it only returns a constant based on whether the user has been created or updated.

To make it clearer to the users who will read this question: User::create() returns the created user entity. It cannot return a value that says whether the user object has been created or updated, as User::create() doesn't save the user object in the database, nor does it check there is a duplicated user object in the database.
It's User::save() that returns that value, but that isn't an issue, thought, since the value returned from User::create() should be stored in a variable. The following code is wrong code to use to create a new user entity.

User::create(['mail' => $email, 'name' => $name])->save();

The call to save() should not be concatenated to the call to create() when the code also needs to access the properties of the created user entity, such as its ID. To achieve that, the code should be similar to the following one.

$user = User::create(['mail' => $email, 'name' => $name]);
$user->save();

Alternatively, the following code is equivalent.

$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage('user');
$user = $storage->create(['mail' => $email, 'name' => $name]);
$user->save();

Is there any way to get the user ID after the user has been created?

After calling $user->save(), the user ID is returned from $user->id().

0

To get the ID from User::create(); use the id() function after the save() function:

$user = \Drupal\user\Entity\User::create();
$user->setPassword(user_password());
$user->enforceIsNew();
$user->setEmail($user_data['user_email']);
$user->setUsername($name);

//Save user
$res = $user->save();
//$res returns the SAVED_NEW or SAVED_UPDATED constant no good for an ID

//But the id function should pass the newly created ID
$newid = $user->id();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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