As the title says, I am trying to get the values from a custom field I added to the user profile. I am using this code from a previously asked question regarding this.

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

This seemed to work for the person that previously asked this question but the problem I am running into is that it is only returning values of an anonymous user regardless of who I am logged in as. For example if I replace custom_field with uid it will return a value of '0'. Any idea where I could be going wrong?

  • Install Devel module, and write dsm($user); above the code you have already, then reload the page, click on the yellow bar, and tell us what comes up.
    – Collins
    Aug 11, 2016 at 19:24

1 Answer 1


You're missing a $ in your second line.. Should be $user->

I always use the below.

global $user;
$user = user_load($user->uid)

And then get the field like your last line. If someone else could confirm if that is a waste and there's a better way to load the field, that'd be great.

  • I think its a typo by @jecunningham2281. Aug 12, 2016 at 5:28

Your Answer

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

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