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->id();
$r_period = $userCurrent->field_r_period_length_sec;

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

Notice: Undefined property: DrupalCoreSessionAccountProxy::$field_r_period_length_sec

What am I doing wrong?

قالب وردپرس