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 = &$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 > 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 < ($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, &$form_state) {
  return drupal_rebuild_form($form_state['build_info']['form_id'], $form_state, $form);

drupal_rebuild_form() will change my other functionality.

  • Don't know if that helps but when I had to work with field collection I managed to alter my field collection field using hook_field_widget_form_alter instead of hook_form_alter.
    – Ismini
    Jun 9, 2017 at 9:36
  • I already try this, Not working for me. I need to add items in node/add/story.. Jun 9, 2017 at 9:55
  • But you don't have the tttttt() function inside your hook_form_alter() hook, do you? This is just because of unlucky code highlighting, right? As it needs to be defined outside the hook_form_alter() function.
    – leymannx
    Jun 21, 2017 at 15:07

1 Answer 1


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.

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

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

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

Hope it helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.