diff --git a/field_collection.module b/field_collection.module index 5692657..e23d9b5 100644 --- a/field_collection.module +++ b/field_collection.module @@ -826,6 +826,7 @@ function field_collection_field_info() { 'settings' => array( 'path' => '', 'hide_blank_items' => TRUE, + 'hide_initial_items' => FALSE, ), // Add entity property info. 'property_type' => 'field_collection_item', @@ -889,6 +890,19 @@ function field_collection_field_settings_form($field, $instance) { ), ), ); + $form['hide_initial_items'] = array( + '#type' => 'checkbox', + '#title' => t('Hide initial items'), + '#default_value' => $field['settings']['hide_initial_items'], + '#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' => 10, + '#states' => array( + // Hide the setting if the cardinality is 1. + 'invisible' => array( + ':input[name="field[cardinality]"]' => array('value' => '1'), + ), + ), + ); return $form; } @@ -1402,7 +1416,7 @@ function field_collection_field_widget_form(&$form, &$form_state, $field, $insta } elseif (!empty($field['settings']['hide_blank_items']) && $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; } @@ -1473,6 +1487,11 @@ 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 if set, force user to explicitly add a fc + if($field['settings']['hide_initial_items'] && !isset($form[$field_name][$element_langcode][0]['#entity']->item_id)){ + unset($form[$field_name][$element_langcode][0]); + unset($form_state['field']['#parents'][$field_name][$element_langcode][0]); + } } } }