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
Comment | File | Size | Author |
---|---|---|---|
#14 | default_values_are_empty.patch | 2.08 KB | Peter Haight |
Comments
Comment #1
fagocommitted related #1614578: Allow other modules to determine if collection item is empty, avoid saving empty item or rendering empty collection
Comment #2
gmclelland CreditAttribution: gmclelland commentedAs 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
Comment #3
radiobuzzer CreditAttribution: radiobuzzer commentedSubscribe
Comment #4
klonos@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
Comment #5
radiobuzzer CreditAttribution: radiobuzzer commented@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 :-)
Comment #6
John Pitcairn CreditAttribution: John Pitcairn commentedComments 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.
Comment #7
radiobuzzer CreditAttribution: radiobuzzer commentedI would indeed opt for the non-standard behaviour and since this is configurable, even better.
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.
Comment #8
John Pitcairn CreditAttribution: John Pitcairn commentedI 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.
Comment #9
RobW CreditAttribution: RobW commentedBig +1. A minor break in ui consistency is a fine trade off for field collections working with default and required value fields.
Comment #10
gmclelland CreditAttribution: gmclelland commentedI 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.
Comment #11
fagoImo 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.
Comment #12
RobW CreditAttribution: RobW commentedCreated a new issue for #8, etc. in #1788222: Allow "add another" button instead of empty field collection item form when using the embedded widget, and linked to from #1788228: [Meta] Field collection and required or default value fields collected issues and solutions overview.
Comment #13
GaëlGFor Feeds, this can be helpful: http://drupal.org/node/1063434#comment-6578468
Comment #14
Peter Haight CreditAttribution: Peter Haight commentedI 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.
Comment #14.0
Peter Haight CreditAttribution: Peter Haight commentedrevised issue summary
Comment #15
fagoI updated the proposed resolution to what's needed:
Comment #15.0
fagoupdated proposed resolution
Comment #16
vadym.kononenko CreditAttribution: vadym.kononenko commentedNice proposal, why it still was not impelmented?
Comment #17
ScratchFury CreditAttribution: ScratchFury commentedI would find this useful, any chance of it getting added at some point?
Comment #18
fago>Nice proposal, why it still was not impelmented?
Patches are welcome!
Comment #19
yareckon CreditAttribution: yareckon commentedfor those looking to get this done in code without as much typing : #2529180: Pass non empty fields to field_collection_is_empty alter hook to save work, increase interoperability