By Fawad Ali


2019-06-10 05:12:39 8 Comments

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(&$form, FormStateInterface $form_state, $form_id) { 
if($form_id == 'my_form'){

for($i = 0; $i < 2; $i++) {

$new_experience_item = FieldCollectionItem::create(['field_name' => '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?

1 comments

@4k4 2019-06-11 06:21:22

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->field_collection_name[] = $new_item;

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

@Fawad Ali 2019-06-11 06:33:08

Let me tell you my case. I have a field in profile named Experience and the field is field collection and unlimited.When the user apply to a job so I want to copy all the experience from the user profile. For this purpose I made a new field collection field and on the job page I load all the experience and want to create new field collection items to store that experience data.

@Fawad Ali 2019-06-11 06:35:28

for reference you can see this drupal.stackexchange.com/questions/268421/…

@4k4 2019-06-11 06:41:16

Then do this in a controller, see drupal.stackexchange.com/questions/236871/…

@Fawad Ali 2019-06-11 06:45:12

I want this for field collection item ... Sorry I am very new to Drupal 8 module development

@4k4 2019-06-11 06:48:36

Use the code from your link and put it in a controller like in my link.

Related Questions

Sponsored Content

0 Answered Questions

Field collection required field is not working

  • 2019-08-06 05:01:15
  • karthick
  • 20 View
  • 0 Score
  • 0 Answer
  • Tags:   8 forms

1 Answered Questions

[SOLVED] Language specific settings for field formatters

  • 2018-09-24 10:59:34
  • Hudri
  • 215 View
  • 2 Score
  • 1 Answer
  • Tags:   8

1 Answered Questions

[SOLVED] Create new child field collection programmatically

  • 2018-08-30 07:57:36
  • user66981
  • 559 View
  • 0 Score
  • 1 Answer
  • Tags:   8 entities

3 Answered Questions

[SOLVED] How to give custom function in D8 custom field widget class?

  • 2016-08-16 10:09:40
  • Jitha M Saroj
  • 814 View
  • 0 Score
  • 3 Answer
  • Tags:   8

1 Answered Questions

[SOLVED] How to validate all elements in Custom Widget

  • 2016-08-19 06:18:23
  • Jitha M Saroj
  • 697 View
  • 1 Score
  • 1 Answer
  • Tags:   8

0 Answered Questions

Custom Fields not displayed

  • 2016-08-10 11:53:46
  • Jitha M Saroj
  • 775 View
  • 1 Score
  • 0 Answer
  • Tags:   8

0 Answered Questions

#states in formElement for a FieldWidget

  • 2016-10-28 20:32:41
  • Malabya Tewari
  • 112 View
  • 3 Score
  • 0 Answer
  • Tags:   8 entities

2 Answered Questions

0 Answered Questions

How to access the values of custom Widget form fields?

  • 2016-08-25 10:37:31
  • Jitha M Saroj
  • 761 View
  • 0 Score
  • 0 Answer
  • Tags:   8

2 Answered Questions

[SOLVED] How to debug hook_form_BASE_FORM_ID_alter not working?

  • 2014-07-06 20:15:16
  • Free Radical
  • 183 View
  • 0 Score
  • 2 Answer
  • Tags:   7 hooks

Sponsored Content