4

I added a field collection to the user 'manage field' page, the field machine name is 'houses'. The user may add as many home he want.

This field collection regroup :

  • a sample text plain field (the name of the house), the machine name "house_name"
  • a sample integer field (the size of the house), the machine name "house_size"

In a custom module, I want load the current user and get values of all values of all his houses (in an array ?), how can I do that ?

I tried this :

$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$user_houses = $user->get('field_houses')->getValue();
ksm($user_houses);

But I get :

enter image description here

It's ok, I set 8 houses in "user edit" page, so I get an array of 8 entries. But, I would like get all data in an array like this :

house 1 : name1 : size1
house 2 : name2 : size2
...
house 8 : name8 : size8

Thank you :)

4

I find =)

With :

use Drupal\field_collection\Entity\FieldCollectionItem;
...
$test = FieldCollectionItem::load(3);

Where "3" is, by example, a value returned by $user->get('field_houses')->getValue();

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.