Drupal 8, save new fieldCollection items to user from a foreach loop doesn't work My user structure have a multiple field "foo" of type "FieldCollection". From a custom module, I want create programmatically several fieldcollection item and add them to the current user. 

Without any loop, when I add only one new FieldCollection item to the user, its work. But, when I want user a loop for add several FieldCollection, only the first item is added. I mean : 

With some light php :  

// In my module
    // some operations and prepare the datas structure
    foreach(datas as data){ // count(datas) = 2, by example
        $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
        $field_collection_foo = FieldCollectionItem::create(array('field_name' =&gt; "field_foo"));
        $field_collection_foo-&gt;set("field_data",$data);       
        $field_collection_foo-&gt;setHostEntity($user);
        $field_collection_foo-&gt;save();
    }


Then if I go to the user edit page from the back office, I see only the item datas[0] added, not datas[0] datas[1]

Where is my mistake ? 

UPDATE (add some test) : 

I think I find, I do an another $user->save somewhere in my module and I think its it who save the wrong value :) Ill test it
Make sure you've configured the cardinality of the field collection foo as unlimited (or at least anything more than 1) under "Field settings".

Also double check the name that you are passing to the FieldCollectionItem::create() method. It shouldn't be the label name but the machine name of your foo field collection, which will probably be field_foo:

$field_collection_foo = FieldCollectionItem::create(
  array('field_name' =&gt; "field_foo")
);


This also applies to the subfield_data (in your field collection item):

$field_collection_foo-&gt;set("field_subfield_data", $data);