Motovation

This issue is forked from the comments in #1239946: Embedded field collection items with a default value result in new items on save and #1662998: Create configuration option to ignore chosen collected fields when checking for an empty field collection. There have been many requests for the following functionality:

  • Using the embedded widget, field collection shows only created collections with an "add another item" button.
  • When no created collections exist, field collection shows an "add an item" button, and nothing else.

Proposed Solution

This was prototyped in #1239946-39: Embedded field collection items with a default value result in new items on save, but needs to be reworked as its own option for the new dev branch.

See also #1788228: [Meta] Field collection and required or default value fields collected issues and solutions overview.

Files: 
CommentFileSizeAuthor
#12 field_collection-allow-add-another-1788222-12.patch2.21 KBczigor
PASSED: [[SimpleTest]]: [MySQL] 163 pass(es).
[ View ]
#6 field_collection-allow-add-another-1788222-6.patch2.21 KBDaneMacaulay
FAILED: [[SimpleTest]]: [MySQL] 159 pass(es), 6 fail(s), and 8 exception(s).
[ View ]

Comments

RobW’s picture

Issue summary:View changes

Added reference to the meta

coredumperror’s picture

I really need this functionality for my site, and I'd be more than happy to simply patch my local copy of field_collection to fix this issue, but I have no idea which section of code from the various patches on that page contains the removed prototype of this functionality. Could someone please point me in the right direction, so that I can patch field_collection to quiet the complaints from my users?

servantleader’s picture

Are this and related issues going to be addressed, or has development on this module been abandoned? Without fixing this, the module is only usefull for simple use cases (it is almost useless if you want a radio select without a N/A option). I would start writting the code myslef if I knew that it would be reviewed and commited, if not, I need to find a diferent solution than this module.

hhebert’s picture

Is there any development about this issue ?

coredumperror’s picture

I'm fairly sure that the underlying problem here was fixed in the December release (7.x-1.0-beta5), but I'm not positive. I remember being very happy to see that something related to this had changed for the better, but I can't recall what, exactly.

hhebert’s picture

With the last version of Field_collection and Entity, I still have the problem of an empty field_collection show in my form when i have required fields instead of a "Add another" button only.

DaneMacaulay’s picture

Status:Active» Needs review
StatusFileSize
new2.21 KB
FAILED: [[SimpleTest]]: [MySQL] 159 pass(es), 6 fail(s), and 8 exception(s).
[ View ]

attached adds setting to remove initial field collection form

tkbcreative’s picture

@DaneMacaulay - thank you very much - your patch in #6 solved this issue for me

steven.wichers’s picture

For single value collections the patch in #6 does not appear to do anything other than remove the field collection forms entirely.

steven.wichers’s picture

Issue summary:View changes

headers

valentin schmid’s picture

Issue summary:View changes

Patch #6 solves the issue perfectelly. Patched against 7.x-1.0-beta7.
Thank you!

Status:Needs review» Needs work

The last submitted patch, 6: field_collection-allow-add-another-1788222-6.patch, failed testing.

czigor’s picture

Status:Needs work» Needs review
StatusFileSize
new2.21 KB
PASSED: [[SimpleTest]]: [MySQL] 163 pass(es).
[ View ]

Maybe changing the default value to FALSE will make pass the tests. Also existing sites relying upon showing the first blank item won't break.

mlncn’s picture

Status:Needs review» Reviewed & tested by the community

Fantastic work! This is a lifesaver. Going into production now (patched 7.x-1.x-dev and yes running that), hope it will be committed soon.

Adam Wood’s picture

There are issues with using this patch with the Field Collection Node Clone module. When you clone a node with field collections using this patch, it will remove the first item in every field collection.

I haven't had a chance to look into it further yet, however I thought I'd flag it up. We just had to temporarily remove this functionality whilst cloning content.

gagarine’s picture

Patch #12 works perfectly. Thanks.

jmuzz’s picture

Status:Reviewed & tested by the community» Needs work
+++ b/field_collection.module
@@ -889,6 +890,19 @@ function field_collection_field_settings_form($field, $instance) {
+    '#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."),

Use single quotes when possible for consistency with Drupal code standards and the rest of the module.

+++ b/field_collection.module
@@ -826,6 +826,7 @@ function field_collection_field_info() {
+        'hide_initial_items' => FALSE,

@@ -889,6 +890,19 @@ function field_collection_field_settings_form($field, $instance) {
+  $form['hide_initial_items'] = array(
...
+    '#title' => t('Hide initial items'),

@@ -1473,6 +1487,11 @@ function field_collection_field_attach_form($entity_type, $entity, &$form, &$for
+      if($field['settings']['hide_initial_items'] && !isset($form[$field_name][$element_langcode][0]['#entity']->item_id)){

It should be singular since there will only be one initial item.

+++ b/field_collection.module
@@ -889,6 +890,19 @@ function field_collection_field_settings_form($field, $instance) {
+    '#states' => array(
+      // Hide the setting if the cardinality is 1.
+      'invisible' => array(
+        ':input[name="field[cardinality]"]' => array('value' => '1'),
+      ),

@@ -1473,6 +1487,11 @@ function field_collection_field_attach_form($entity_type, $entity, &$form, &$for
+      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]);
+      }

I think it needs a similar condition for hide blank items. The option doesn't make sense if hide blank items isn't checked so it shouldn't appear or work when it's not.

+++ b/field_collection.module
@@ -1402,7 +1416,7 @@ function field_collection_field_widget_form(&$form, &$form_state, $field, $insta
-        // 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.

@@ -1473,6 +1487,11 @@ function field_collection_field_attach_form($entity_type, $entity, &$form, &$for
+      // remove blank form elements if set, force user to explicitly add a fc

Capitals letters at the start and periods at the end please.

milos.kroulik’s picture

Patch from #12 doesn't apply to beta8 anymore.

czigor’s picture

It applies to dev.

milos.kroulik’s picture

Sorry, it seems to me, that latest dev is currently identical with beta 8:

http://cgit.drupalcode.org/field_collection/log/

czigor’s picture

How are you trying to apply the patch? For me it applies without problem.

liquidcms’s picture

from original issue description i see:

When no created collections exist, field collection shows an "add an item" button, and nothing else.

would this suggest that when i am first creating a node which has a FC field with number of values = 1; i should see ONLY an "Add" button and no initial FC form? Even though the field setting states:

Remove the blank item that is always added to any multivalued field's form. If checked, user must explicitly add the field collection.

it sounds as though this would only apply for multivalued items. (and sure enough i do not get an Add button for my field but the single FC edit form as i used to get prior to patch)

i am not sure why we wouldn't want an Add button EVERY TIME when we state we want an Add button..