Get the value of a custom user field I have a custom form that needs to display a default value from a custom user field. The field was created under /admin/config/people/accounts/fields and its machine name is field_r_period_length_sec.

//get user
$userCurrent = \Drupal::currentUser();
$uid = $userCurrent-&gt;id();
$r_period = $userCurrent-&gt;field_r_period_length_sec;


However, the value doesn't show and I get this error:


  Notice: Undefined property: Drupal\Core\Session\AccountProxy::$field_r_period_length_sec


What am I doing wrong?
Berdir's answer gave me a search term that got me this:

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


After a bit of trial and error, this is the code that works, in case anyone else needs:

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
$r_period = $user-&gt;field_r_period_length_sec-&gt;value;


For multiple values, use

$r_period = $user-&gt;get('field_r_period_length_sec')-&gt;getValue();