How do I add field collection unlimited items from hook_form_alter()? I am trying to implement what described in Add unlimited field collections in a from of drupal and Field collection with unlimited cardinality pre-populate fields, but the Add more and Remove buttons don't work. I have issues with drupal_rebuild_form().

The code in hook_form_alter() is the following.

$items_count = 3;
$field_name = 'field_story_technology';
$form_theme = $form[$field_name][LANGUAGE_NONE]['#theme'];
// Remove delete and add more buttons
//~ $form_state['field'][$field_name][LANGUAGE_NONE]['items_count'] = $items_count;
$items = &amp;$form_state['field'][$field_name][LANGUAGE_NONE];
$form[$field_name]['und'][0]['remove_button']['#access'] = TRUE;
$form[$field_name]['und']['add_more']['#access'] = TRUE;
// Generate required number of fields collection
if ($items_count &gt; 1 and $items['items_count'] != $items_count) {
  $items['items_count'] = $items_count;
  $items['field']['cardinality'] = FIELD_CARDINALITY_UNLIMITED;//$items_count;
  $form[$field_name][LANGUAGE_NONE] = field_multiple_value_form($items['field'], $items['instance'], LANGUAGE_NONE, array(), $form, $form_state);
  // Reset theme function, as field_multiple_value_form hijacks it
  $form[$field_name][LANGUAGE_NONE]['#theme'] = $form_theme;
  $rebuild_p = TRUE;
  //~ $form_state['rebuild'] = TRUE;
}
// Fill generated fields with data
for ($delta = 0; $delta &lt; ($items_count-1); $delta++) {
  $form['field_story_technology'][LANGUAGE_NONE][$delta]['field_technology_photo_title'][LANGUAGE_NONE][0]['value']['#default_value'] = 'new value';
}
if ($rebuild_p) {
  $form['#after_build'][] = 'tttttt';
}

function tttttt($form, &amp;$form_state) {
  return drupal_rebuild_form($form_state['build_info']['form_id'], $form_state, $form);
}


drupal_rebuild_form() will change my other functionality.
here in my below example i want to attach 5 images which will save in admin field collection , so here is code for it . here 109is node id in which i want to save field collection value. field_manager_update is my field collection name.

&lt;?php $images = array(//images to attach to node
    '0' =&gt; 'http://cf.juggle-images.com/matte/white/280x280/drupal-logo-primary.jpg',
    '1' =&gt; 'http://cdn.flaticon.com/png/256/37187.png',
    '2' =&gt; 'https://www.drupal.org/files/project-images/logo-128x128.jpg',
    '3' =&gt; 'http://guillermocerezo.com/sites/default/files/field/image/drupal-logo_0.jpg',
    '4' =&gt; 'http://drupalcrafters.org/sites/default/files/DrupalCraft-logo_0.png'
  );

$node = node_load(109);

$field_collection_value = entity_create('field_collection_item', array('field_name' =&gt; 'field_manager_update'));

// Attach to the node
$field_collection_value-&gt;setHostEntity('node', $node);

for ($i = 0; $i &lt; sizeof($images); $i++) {        
    $file_info = system_retrieve_file($images[$i], 'public://pictures/', TRUE, FILE_EXISTS_RENAME); //storing image inside picture folder
    if ($file_info-&gt;fid) {//if fid exist then image is saved from URL.
      //$node-&gt;field_prop_land_image[LANGUAGE_NONE][$i]['fid'] = $file_info-&gt;fid; //assign
      $field_collection_value-&gt;field_prop_update_image[LANGUAGE_NONE][$i]['fid'] = $file_info-&gt;fid;
    }        
}    
// Save field-collection item.
$field_collection_value-&gt;save();  ?&gt;


Hope it helps :)