Motivation

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
#33 field_collection-allow-add-another-1788222-33.patch3.26 KBmvlabat
#29 field_collection-allow-add-another-1788222-29.patch2.48 KBlogaritmisk
PASSED: [[SimpleTest]]: [MySQL] 166 pass(es). View
#24 field_collection-allow-add-another-1788222-24.patch2.47 KBChris Burge
PASSED: [[SimpleTest]]: [MySQL] 166 pass(es). View
#22 field_collection-allow-add-another-1788222-22.patch2.2 KBNick Bell
PASSED: [[SimpleTest]]: [MySQL] 166 pass(es). View
#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
FileSize
2.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
FileSize
2.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..

Nick Bell’s picture

Issue summary:View changes
FileSize
2.2 KB
PASSED: [[SimpleTest]]: [MySQL] 166 pass(es). View

Apologies for amateurism, don't know how to propose a new patch properly (I have just attached mine) but patch #12 didn't work with 7.x-1.0-beta8+11-dev until I made the following tweak:

@@ -1402,7 +1416,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;
       }

I'm not quite clear about the "hide blank item" vs "hide initial item" distinction but ticking both boxes achieves what I want.

Nick

Nick Bell’s picture

(accidentally created comment - please delete)

Chris Burge’s picture

Status:Needs work» Needs review
FileSize
2.47 KB
PASSED: [[SimpleTest]]: [MySQL] 166 pass(es). View

jmuzz posted some fixes that need to made when he changed the issue status to 'Needs Work'. The attached patch implements those fixes.

  • Double quotes have been replaced with single quotes.
  • 'hide_initial_items' has been renamed 'hide_initial_item'.
  • 'hide_initial_item' is only shown on the form only if 'hide_blank_items' is TRUE.
  • Initial item is only removed if 'hide_blank_items' is TRUE and if 'cardinality' is -1.
  • Comment syntax has been corrected
    • "add button is pressed the item count will be 2 and we show two items." is the second line of a multi-line comment
  • Patch has been run through Coder.
Chris Burge’s picture

Regarding the Field Collection Clone module, we can open an issue to address the issue described by #14 once this patch has been committed.

milos.kroulik’s picture

Patch in #24 seems to be working fine.

meecect’s picture

#24 works, however, it would be nice if we had control of the button text. I use the custom add another module, which allows you to set the text that appears on the default 'add another' and 'remove' button. This module adds a third possibility, but we can't control the text. In my case, it's a form that is adding children, so my buttons normally say 'add another child', and 'remove child'.

When I enable this module, the button initially says 'Add another item', and then if i click it, I see the form for the blank item, but the button changes to 'add another child'.

Chris Burge’s picture

@meecect - Have you tried using hook_form_alter?

logaritmisk’s picture

FileSize
2.48 KB
PASSED: [[SimpleTest]]: [MySQL] 166 pass(es). View

I hade issues that the first item was detected as empty, when it wasn't. This was only when host node was new (not saved yet).
It was an easy fix, I switched !isset($form[$field_name][$element_langcode][0]['#entity']->item_id) to field_collection_item_is_empty($form[$field_name][$element_langcode][0]['#entity'])

milos.kroulik’s picture

Status:Needs review» Needs work

#27 seems to seems to be a regression. The patch shouldn't limit other modules.

Chris Burge’s picture

Status:Needs work» Needs review

The issue described by #27 is not a regression. The "Custom Add Another" module is a dependent module. Custom Add Another needs to account for the change made by this patch downstream when it implements hook_preprocess_field_multiple_value_form().

ashzade’s picture

Thank you! Patch #29 with Custom Add Another is a great combination. The only issue I see that the "Order" column is shown until I add the first row.

mvlabat’s picture

Applying the previous patch breaks the field collection subfields which bave their (css/js) attachements.
You can test this, for instance, with Geofield and Addressfield Autocomplete combination.
To reproduce it you have to add a field collection containing geofield populated from Addressfield Autocomplete field.

This patch fixes this issue.

Valentine94’s picture

Looks nice to me. +1 to RTBC.