Create new child field collection programmatically I have 2 field collections (Parent and child)


Parent Field collection: Test Student Info
machine name: field_test_student_info
Child Field collection: Marks
machine name: field_marks


In parent field collection have 1 field:


Field1: Student Name
machine name: field_student_name


In child field collection have 2 fields:           


Field1: English Marks
machine name: field_eng_marks
Field2: History Marks
Machine name: field_history_marks


I have set field setting "unlimited" to child field collection (Marks). I just want to add new child field collection item programmatically.
I have tried below code but it's showing me error:

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\field_collection\Entity\FieldCollectionItem;

function CreateChildFC() {
  $node = \Drupal\node\Entity\Node::load(770);
  $load = FieldCollectionItem::load(207); // Loading child FC from parent FC i.e Marks
  $field_collection_items = $load-&gt;field_marks;
  $field_collection_item = FieldCollectionItem::create(
   [
    'field_name' =&gt; 'field_marks', 
   ]
   );
  $field_collection_item-&gt;setHostEntity($node);
  $field_collection_item-&gt;set('field_eng_marks', '89');
  $field_collection_item-&gt;set('field_history_marks', '75');
  $load-&gt;field_marks[] = ['field_collection_item' =&gt;   $field_collection_item];
  $node-&gt;save();
}


Error
The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to _field_collection_field_item_list_full() must implement interface Drupal\Core\Field\FieldItemListInterface, null given, called in /site/modules/contrib/field_collection/src/Entity/FieldCollectionItem.php on line 333 in _field_collection_field_item_list_full() (line 151 of modules/contrib/field_collection/field_collection.module).
You try the code:

&lt;?php 
use Drupal\field_collection\Entity\FieldCollectionItem;
use Drupal\node\Entity\Node;

[.....]

  $node = Node::load(7);
  $field_collection_item = FieldCollectionItem::create(
    [
      'field_name' =&gt; 'field_test_student_info',
    ]
  );

  $field_collection_item-&gt;set('field_student_name', 'Student Name');

  $field_mark_collection = FieldCollectionItem::create(['field_name' =&gt; 'field_marks']);
  $field_mark_collection-&gt;set('field_english_marks', '1231');
  $field_mark_collection-&gt;set('field_history_marks', '123');
  $field_collection_item-&gt;field_marks[] = ['field_collection_item' =&gt; $field_mark_collection];
  $node-&gt;field_test_student_info = ['field_collection_item' =&gt; $field_collection_item];
  $field_collection_item-&gt;setHostEntity($node);
  $node-&gt;save();


You need create new collection field  FieldCollectionItem::create(['field_name' =&gt; 'field_marks']) 

You can set value to the file child in field parent collection, it is same you when set value field collection in node, here you dont's need set setHostEntity, it will understand is the field_collection_item

As the code above I also checked it, and it worked.

You can refer to it here 

Goodluck