Score:-1

How do I get the (integer) uid from the User entity?

kz flag

On the odd chance there was a "getUid()" method for the user entity, I tried it, but got:

Call to undefined method Drupal\user\Entity\User::getUid()

Is there a way to get the uid from the user entity, without loading the full User object?

Jaypan avatar
de flag
If you have the user entity, it's already loaded, and you can get the ID from it. Drupal 7: `$user->id` Drupal 8+: `$user->id()`.
Score:2
de flag

If you have the user entity, it's already loaded, and you can get the ID from it.

  • Drupal 7

    $user->uid
    
  • Drupal 8 and later

    $user->id()
    
apaderno avatar
us flag
This works also with the object returned from `\Drupal::currentUser()`. If the code wants to avoid to load the full User object, and it needs the ID for the currently logged user, that's the code to use. It doesn't seem the OP case, since the error is for `Drupal\user\Entity\User`, which means the code is trying to call `getUserId()` on a `Drupal\user\Entity\User` object, not a `Drupal\Core\Session\AccountProxy` object. The _without loading the full User object_ part seems to contradict what the code is doing.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.