19

$account = \Drupal::currentUser() loads the user object into $account, but how do I access custom fields I've created on the user object (via the admin UI)?

With dpm($account) I can't see any of my custom fields.

  • Do the custom fields have any values yet? Seem to remember them being missing from the entity object until they actually have a value set – Clive Feb 3 '15 at 15:06
  • Yes, they have values. I can get the uid with \Drupal::currentUser()->id() and then do a database query, but in D7 they were there on the object already? – 24ma13wg Feb 3 '15 at 15:16
  • This issue isn't just about the currant user; it pertains generally to the user object. api.drupal.org/api/drupal/… gets you halfway there, but as is usually the case with Drupal Docs, it leaves much still in the dark – sea26.2 Jan 30 '19 at 18:16
30

\Drupal::currentUser() returns an Drupal\Core\Session\AccountInterface object. That could be a full user (that would be a Drupal\user\UserInterface), but right now, for the default authentication method, it is not.

Use this to get the user entity and all the fields:

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id())

D7 had exactly the same behavior, the difference now is just that in D7, they were simply different stdClass'es, now you have explicit interfaces to differentiate the two.

  • Despite the down-vote, this answer is correct, even about Drupal 7. $GLOBALS['user'] doesn't contain a full loaded user object, in Drupal 7; it contains only the information found in the users table. – kiamlaluno Apr 26 '16 at 7:59
  • Yeah, not sure why this got a downvote. It is correct, both about D7 and D8. – mpdonadio Apr 26 '16 at 14:50
  • This is only part of the answer. And in fact, it gets you no closer to the data than the original "solution". You have to "get" the data from the object. To do that - see the working solution posted by @batigolix below. – sea26.2 Jan 30 '19 at 18:14
  • Note that fields that don't have values will not appear in the values array. – JAyenGreen Sep 5 '19 at 14:24
17

And here is how to get it out of that object:

// Load the current user.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

// Get field data from that user.
$website = $user->get('field_website')->value;
$body = $user->get('body')->value;

// Some default getters include.
$email = $user->get('mail')->value;
$name = $user->get('name')->value;
$uid= $user->get('uid')->value;
  • 1
    You get the user ID with $user->id(). – kiamlaluno Apr 26 '16 at 8:00
  • Also, the User entity doesn't have a body base field. If that is a field added from the user interface, the field name would start with field_. I would avoid that example, as it makes users think that is a base field added from Drupal core code. – kiamlaluno Sep 2 '19 at 12:41
1

Drupal::currentUser() returns an object implementing Drupal\Core\Session\AccountProxyInterface. That is different from what returned from User::load(), which is an object implementing Drupal\user\UserInterface.

This means it's not assured you get a full User object with its fields loaded, but it's not even assured you get a User object. As consequence, you call call some of the methods available from the User class, such as User::get(). You can call AccountInterface::id(), which returns the user ID; with that, you can then load the full User object with User::load() and have access to all the fields modules attached to the entity.

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\user\Entity\User;

$account = User::load(\Drupal::currentUser()->id());

With $account, you can then access all the fields associated with a user account.

  • Perhaps, but it still begs the question: "how do you access the CUSTOM fields". See answer above from @batigolix – sea26.2 Jan 30 '19 at 18:20
  • The question is more Why $account = \Drupal::currentUser(): dpm($account); doesn't show any custom fields? but this answer says how to access user entity fields. Since the question doesn't say which fields the OP is trying to access, the answer just says which method to use. There aren't answers from @batigolix. – kiamlaluno Jan 30 '19 at 19:48
-2

Here's one-liner, using the API function:

$account = \Drupal::currentUser()->getAccount();

After that, the user fields will be accessible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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