function field_collection_field_attach_form in Field collection 7
Implements hook_field_attach_form().
Corrects #max_delta when we hide the blank field collection item.
See also
field_collection_field_widget_form()
File
- ./
field_collection.module, line 1467 - Module implementing field collection field type.
Code
function field_collection_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
foreach (field_info_instances($entity_type, $form['#bundle']) as $field_name => $instance) {
$field = field_info_field($field_name);
if ($field['type'] == 'field_collection' && field_collection_hide_blank_items($field) && field_access('edit', $field, $entity_type)) {
$element_langcode = $form[$field_name]['#language'];
if (!empty($form[$field_name][$element_langcode]['#max_delta']) && $form[$field_name][$element_langcode]['#max_delta'] > 0) {
$form[$field_name][$element_langcode]['#max_delta']--;
}
// Remove blank form elements and force user to explicitly add a field
// collection if both 'hide_initial_item' and 'hide_blank_items' are TRUE.
if ($field['settings']['hide_initial_item'] && $field['settings']['hide_blank_items'] && !empty($form[$field_name][$element_langcode][0]['#entity']) && $form[$field_name][$element_langcode][0]['#entity'] instanceof FieldCollectionItemEntity && field_collection_item_is_empty($form[$field_name][$element_langcode][0]['#entity'])) {
_field_collection_process_children_attached($form[$field_name][$element_langcode][0]);
unset($form[$field_name][$element_langcode][0], $form_state['field']['#parents'][$field_name][$element_langcode][0]);
}
}
if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED && empty($form_state['programmed']) && $instance['widget']['type'] === 'field_collection_embed' && field_access('edit', $field, $entity_type) && $field['type'] === 'field_collection') {
$element_langcode = $form[$field_name]['#language'];
$element_wrapper = $form[$field_name][$element_langcode]['add_more']['#ajax']['wrapper'];
for ($i = 0; $i <= $form[$field_name][$element_langcode]['#max_delta']; $i++) {
if (isset($form[$field_name][$element_langcode][$i]['remove_button'])) {
$form[$field_name][$element_langcode][$i]['remove_button']['#ajax']['wrapper'] = $element_wrapper;
}
}
}
}
// If FCs are translatable, make sure we mark any necessary sub-fields in the
// FC widget as translatable as well.
if ($entity_type === 'field_collection_item' && field_collection_item_is_translatable()) {
foreach (field_info_instances($entity_type, $form['#bundle']) as $field_name => $instance) {
$field = field_info_field($field_name);
if (isset($field['translatable'])) {
$form[$field_name]['#multilingual'] = (bool) $field['translatable'];
}
}
}
}