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.