Drupal 9: How to get value of a custom user field

By Fernando Katsukawa, 24 November, 2022

This should also work for Drupal 8 and Drupal 10 versions.

To get the value of a custom user field is pretty easy, see below:

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

// Textfield.
$name = $user->get('field_name')->value;

// Multiple values.
$names = $user->get('field_names')->getValue();

About the Author

Profile picture for user fkatsukawa

Fernando Katsukawa is a certified senior backend developer, who have been worked with Drupal in the last 12 years and counting...

Saved your precious time? Use the cup below to buy me a coffee!
buy me a coffee - cup