diff --git a/field_collection.module b/field_collection.module index b5cfc07..100c586 100644 --- a/field_collection.module +++ b/field_collection.module @@ -337,6 +337,7 @@ function field_collection_field_info() { 'settings' => array( 'path' => '', 'hide_blank_items' => TRUE, + 'hide_initial_item' => FALSE, ), // Add entity property info. 'property_type' => 'field_collection_item', @@ -400,6 +401,20 @@ function field_collection_field_settings_form($field, $instance) { ), ), ); + $form['hide_initial_item'] = array( + '#type' => 'checkbox', + '#title' => t('Hide initial item'), + '#default_value' => $field['settings']['hide_initial_item'], + '#description' => t('Remove the blank item that is always added to any multivalued field\'s form. If checked, user must explicitly add the field collection.'), + '#weight' => 11, + '#states' => array( + // Show the setting if the cardinality is -1 and hide_blank_items is checked. + 'visible' => array( + ':input[name="field[cardinality]"]' => array('value' => '-1'), + ':input[name="field[settings][hide_blank_items]"]' => array('checked' => TRUE), + ), + ), + ); return $form; } @@ -950,7 +965,7 @@ function field_collection_field_widget_form(&$form, &$form_state, $field, $insta } elseif (field_collection_hide_blank_items($field) && $field_state['items_count'] == 0) { // We show one item, so also specify that as item count. So when the - // add button is pressed the item count will be 2 and we show to items. + // add button is pressed the item count will be 2 and we show two items. $field_state['items_count'] = 1; } @@ -1021,6 +1036,15 @@ function field_collection_field_attach_form($entity_type, $entity, &$form, &$for if ($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'] + && field_collection_item_is_empty($form[$field_name][$element_langcode][0]['#entity']) + ) { + unset($form[$field_name][$element_langcode][0]); + unset($form_state['field']['#parents'][$field_name][$element_langcode][0]); + } } } }