How can I get the value of a custom field added to user profiles?

I read and tested everything I found on Google, including the following code.

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$custom_field_value = $user->field_custom_field->value;
// $custom_field_value = $user->get('field_custom_field')->value;

It does not work, though.

How can I get the value of a custom field in user profiles?

  • 1
    Does this answer your question? Get the value of a custom user field
    – mona lisa
    Nov 29, 2023 at 18:48
  • 1
    Your field is named custom_field or field_custom_field. You can’t refer to the same field by two names.
    – mona lisa
    Nov 29, 2023 at 18:50
  • hi cilefen, your thread and the code provided doenst work. i have already test it. i fixed my message and copy/paste error for the field name, sorry for the mistake
    – user54375
    Nov 29, 2023 at 19:28
  • What happens when it doesn't work? The Question does not say. You should add this information to the Question. There is nothing here to distinguish your question from drupal.stackexchange.com/questions/195238/… except "doesn't work", but that isn't anything to go on.
    – mona lisa
    Nov 29, 2023 at 20:07
  • You've added the field to the user profile, but you are looking for it in the user entity. It's possible you mean the same thing, but since there is a popular module Profile it's not necessarily the same.
    – 4uk4
    Nov 29, 2023 at 21:48

1 Answer 1


If you've added a new field through admin/config/people/accounts/fields and wish to retrieve its value, try the below code:

use Drupal\Core\Session\AccountInterface;

// Get the current user.
$current_user = \Drupal::currentUser();

// Load the user entity.
$user = \Drupal::entityTypeManager()->getStorage('user')->load($current_user->id());

// Check if the user entity exists and has the specified field.
if ($user && $user->hasField('field_custom_field')) {
  // Access the value of the 'field_custom_field' field.
  $custom_field_value = $user->get('field_custom_field')->value;
  // Now $custom_field_value contains the value of the field.
  // You can use it as needed.

else {
  // The user entity or the specified field doesn't exist.
  • 1
    This code is not materially different from the OP’s example.
    – mona lisa
    Dec 5, 2023 at 0:10
  • Hey @cilefen, I'm not sure what's ( OP’s example). In this code I used getStorage.
    – Barry
    Dec 5, 2023 at 0:20
  • They haven’t explained what “doesn’t work” means. Possibly they haven’t loaded a user object, which is what you are insinuating.
    – mona lisa
    Dec 5, 2023 at 0:43
  • Correct. Based on the assumption that a new field was added via admin/config/people/accounts/fields, the goal is to retrieve the value of this field
    – Barry
    Dec 5, 2023 at 0:58

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.