Field collection item code not working Hi Every one I am trying to create new field collection item programmatically but code is not working. I want to create a number of field collection items but no Luck. My code is following.

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

function mymodule_form_alter(&amp;$form, FormStateInterface $form_state, $form_id) { 
if($form_id == 'my_form'){

for($i = 0; $i &lt; 2; $i++) {

$new_experience_item = FieldCollectionItem::create(['field_name' =&gt; 'field_collection_name']);

      }


    }

}


My first Question-> Can we run this code in the hook_form_alter?  If yes, Then why my code is not working perfectly?
Paragraphs or Field Collection doesn't matter for your code. You need to add the newly created items to a field.

For example if the field collection field is in a node:

$node-&gt;field_collection_name[] = $new_item;

Not in a form alter hook, but in an entity create hook. See the linked question.