I created a form in drupal for creating a new node. This content type has a field collection with 2 fields, name and amount.
I use the code below in form and add 5 field collections to form, but when submit the form, only the last field collection is saved in $form_state['values] and 4 elements get lost.
How can I add unlimited field collections to a form by programming?
In my form:
module_load_include('inc', 'field_collection', 'field_collection.pages');
$form['cooking_type']['cooking_materials'] = array();
for ($i=0;$i<5;$i++) {
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_cooking_materials'));
$form['cooking_type']['cooking_materials'][$i] = array();
field_attach_form('field_collection_item', $field_collection_item, $form['cooking_type']['cooking_materials'][$i], $form_state);
}
in submit form:
$material_name= $form_state['values']['field_material_name']['und'][0]['value'];
$material_amount = $form_state['values']['field_material_amount']['und'][0]['value'];