I added a new field in the field manage user page. This is a sample integer multivalue field. (we can enter several integer)
I edited the profile of the admin user. I added (and save) some values in this field :
42
66
23
77
In a custom module, I load the current user and I want display all values of my integer multivalue field :
// Load the current user.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
// Get field data from that user.
$mycustomfield = $user->get('field_mycustomfield');
but when I print $mycustomfield->getValue(), I get only the first value :
// Display values
kint($mycustomfield->getValue()); // return only 42, but I want get 42, 66, 23, 77
How can I get ALL values of a custom field ?
Thanks :)
EDIT
I try this ...
// Load the current user.
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
// Set and save field data from that user.
$user->get('field_mycustomfield')->setValue(array(1,2,3,4,5));
$user->save();
// And display
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$user_mycustomfield = $user->get('field_mycustomfield')->getValue();
kint($user_mycustomfield); // print array(1.2.3.4.5) x)
With a new user, if I set several values from his user profile edit page, getValue() return only the first value
But if I set several values via the api, I can get all values with getValue
It's really strange..