7

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..

  • 1
    I guess it is a caching issue. You probably put the code not in the displayed user, but in a different place. If that is the case, you have no cache tag for the user, if you don't add it yourself. – 4k4 Sep 7 '16 at 16:37
  • I have only one user (the admin), maybe you are right about the caching issue, but I don't understand why I get the first value (caching issue = no value ? why the first but not next values ?) – user2137454 Sep 7 '16 at 17:43
  • What field type is it? Do the values show up again in the edit form? Try $user_mycustomfield[1]->value and so on. – Berdir Sep 7 '16 at 18:04
  • @user2137454, i assumed the cached version had one value. Most times if you have two results with the same code it is caching. – 4k4 Sep 7 '16 at 18:08
  • With Drupal, always make sure that you are not in illusion due to cache. Flush it, reload it or drush it. But get rid of it before coming to any conclusion. Be it twig, css or js. – Vishal Kumar Sahu Dec 18 '18 at 12:39
2
$user->get('field_mycustomfield')->getValue();
1

Drupal fields are represented by the ItemList class (whether they're multi-valued or not). The convenience method getValue() iterates through each item's getValue() method to build an array representation of the data value.

To retrieve a string representation of all the items in the list, you can use ItemList::getString(), which iterates through all the list items and calls getString() on each, then implodes them using ", " for glue.

So, for your example:

var_dump($user->field_mycustomfield->getString());

will output

string(14) "42, 66, 23, 77"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.