0

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 '16 at 19:24
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. – Brian Young Aug 12 '16 at 5:28

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.