Problem

Embedded field collections that have fields with default values are automatically created on the first node save.

Examples of undesired behavior this can cause:

  • On every node edit, a new unlimited field collection would be saved with the default values.
  • If the Feeds module imports nodes that have a field collection field, those nodes would each get a field collection field set with default values.

Proposed resolution

Create an optional method for excluding fields from content and *required* validation checks. When an embedded field collection form checks if there is content in it's collected fields it passes over the "excluded" fields, and does not create a new field collection item if no non-excluded fields have been changed.

This has been already implemented by #1614578: Allow other modules to determine if collection item is empty, avoid saving empty item or rendering empty collection - thus in this issue we just need to take care of the UI.

Original reports

Coming from #1239946: Embedded field collection items with a default value result in new items on save it was suggested to open this as a new issue. see comment http://drupal.org/node/1239946#comment-6417928. It looks like some others have suggested similar things - see http://drupal.org/node/1193004#comment-5799354

and

When having non-empty default values and the embedded widget, this would help to fix #1239946: Embedded field collection items with a default value result in new items on save + allow further improvements as intelligent #required validation. For details, also see my comment at http://drupal.org/node/1239946#comment-6169276. Similar issue for addressfield: #1263316: Configurable non-empty value conditions

CommentFileSizeAuthor
#14 default_values_are_empty.patch2.08 KBPeter Haight
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

gmclelland’s picture

As requested here is another issue summary:

Problem/Motivation

Problem: Embedded field collections that have fields with default values are automatically created on the first node save. What would happen if I used the Feeds module to import a bunch of nodes that have a field collection field? Those nodes would all get that field collection field set, which shouldn't be the case? right?

Proposed resolution

I would like to see an option or a way for embedded field collections that contain fields with default values to not be created initially, unless explicitly added or if the field collection itself is set as required. Maybe the embedded field collections forms should be hidden be default until someone clicks an "Add item" button? I know this is the default way that Drupal Core uses for multivalued fields.

Original report

Coming from #1239946: Embedded field collection items with a default value result in new items on save it was suggested to open this as a new issue. see comment http://drupal.org/node/1239946#comment-6417928

It looks like some others have suggested similar things - see http://drupal.org/node/1193004#comment-5799354

radiobuzzer’s picture

Subscribe

klonos’s picture

@radiobuzzer: Hey "patrida"! Being a d.o member for close to 7 years now, I'd expect you to know that we have a big, green "Follow" button on the top-right and that you no longer need to post "subscribe" comments :p

radiobuzzer’s picture

@klonos Hey patrida. Good that you mentioned, that's definitely not been there since then those 7 years. It seems I have become old and gradually losing eyesight :-)

John Pitcairn’s picture

Comments on #2:

For multi-value unlimited-cardinality field collection fields: If the field collection is non-required, but the field collection contains required fields, the behaviour needs to be non-drupal-standard: do not create an empty field instance on the form. The user must explicitly click "add another item" to add a field instance. This behaviour was prototyped in #1239946: Embedded field collection items with a default value result in new items on save, activated by an additional checkbox on the field configuration form, but was removed prior to commit.

But for single-value or specified-cardinality field collection fields, an automatic solution isn't going to be possible, is it? The specified number of field instances will always be created, and the required fields therein will trigger validation errors. If any fields in the collection have default values, the problem is compounded - how will validation know whether the user just wants the default value? #1614578: Allow other modules to determine if collection item is empty, avoid saving empty item or rendering empty collection may help but currently requires custom module code to implement.

radiobuzzer’s picture

For multi-value unlimited-cardinality field collection fields: If the field collection is non-required, but the field collection contains required fields, the behaviour needs to be non-drupal-standard: do not create an empty field instance on the form

I would indeed opt for the non-standard behaviour and since this is configurable, even better.

But for single-value or specified-cardinality field collection fields

A similar approach could be followed here, but it is even more complicated. Let's say you have an attached field collection with a cardinality of three, but the user wants to only enter one field collection.
In that case, initially you display no field collection and you still have the button "Add another item". But the user is allowed to "add another item" only until the maximum cardinality is reached, then the button disappears.

Afterwards, form validation should only validate the field collections forms that was given and then somehow map each of them to its cardinality index.

So in the example, the user should click "Add another item" once, this one field collection form should be submitted and then this should be stored into field_name_1.

John Pitcairn’s picture

I think one key thing that is apparent from this entire discussion is that following Drupal-standard UI for embedded field collections means there need to be a lot of workarounds in code, and there may continue to be unforeseen situations for specific fields that still result in unexpected behavior, requiring more workarounds.

So why not quit trying to fit a square peg in a round hole, and accept that Drupal-standard UI is unsuitable for field collections? That (re) opens a fairly simple possibility:

The field initially displays no empty item. The button says "add a new item", or "add first item".

If the field already contains one or more items, an empty item is not displayed, and the button can still say "add a new item" if desirable (to keep the code simpler), or could say "add another item" (to be more standard).

If the field cardinality is limited, remove the button when the cardinality is reached.

RobW’s picture

Big +1. A minor break in ui consistency is a fine trade off for field collections working with default and required value fields.

gmclelland’s picture

I agree with these comments. Making the embedded field collection forms display on demand would also make for shorter node edits forms over all.

I'm also using http://drupal.org/project/custom_add_another to customize the "Add another item" text to enhance the usability.

fago’s picture

The field initially displays no empty item. The button says "add a new item", or "add first item".

Imo immediately show the first item form and just intelligently validate/save is the desired behaviour in some situations, but I can see that in some situations not having the empty initial item is preferred. Thus, I can see us supporting this optionally, but let's do so in its own issue.

For this issue, let's focus what it says: Make configurable which fields are checked for treating a field-collection as empty. Once we've that the "immediately show the first item form and just intelligently validate/save" case will work just fine.

RobW’s picture

Title: ignore fields when checking for an empty field-collection » Create configuration option to ignore chosen collected fields when checking for an empty field collection
GaëlG’s picture

For Feeds, this can be helpful: http://drupal.org/node/1063434#comment-6578468

Peter Haight’s picture

I have another approach. It has some drawbacks, but works great for us. Basically I overrode the is_empty logic to return TRUE if all of the fields are either empty or set to default. For us it works great. I think it only doesn't work where a field collection field with all of its fields set to empty or default is a valid data set. For our application that's never true.

Peter Haight’s picture

Issue summary: View changes

revised issue summary

fago’s picture

I updated the proposed resolution to what's needed:

Proposed resolution

Create an optional method for excluding fields from content and *required* validation checks. When an embedded field collection form checks if there is content in it's collected fields it passes over the "excluded" fields, and does not create a new field collection item if no non-excluded fields have been changed.

This has been already implemented by #1614578: Allow other modules to determine if collection item is empty, avoid saving empty item or rendering empty collection - thus in this issue we just need to take care of the UI.

fago’s picture

Issue summary: View changes

updated proposed resolution

vadym.kononenko’s picture

Nice proposal, why it still was not impelmented?

ScratchFury’s picture

I would find this useful, any chance of it getting added at some point?

fago’s picture

>Nice proposal, why it still was not impelmented?

Patches are welcome!

yareckon’s picture