There are special considerations when field collections have collected fields with default or required values, especially if the field collection has multiple or unlimited values and uses the embedded widget.

This issue is for discussion and documentation of how the system works as a whole, and collection of related issues and comments. Work on these features should be done in their own issues, and linked here.

Terms

  • A field collection field is what you add to your content type, and set options for in your content type's "Manage fields" tab.
  • A field collection bundle is each field collection in "Structure -> Field Collections". Bundles are to entities as content types are to nodes.
  • A field collection item is the single entity created when you fill out a field collection item form using the embedded widget and save the node.
  • A field collection item form is the form containing one field collection item's fields when using the embedded widget.
  • Collected fields are the fields attached to the field collection bundle.

Required fields

Required fields work well with field collections.

  • Required field collection fields: At least one field collection item must be saved in order for the node to be saved.
  • Required collected fields: If the field collection item is being saved, every collected field must pass validation, including required fields. If the field collection item is not being saved, the required collected fields don't have to be filled out.

Default Values

Problem: On multiple and unlimited fields, Drupal shows a blank field and an "add another" button. Field collection sees the default collected field values in the embedded widget as user entered content, and creates new field collection items when the node is saved.

Desired functionality

  1. 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.

    Prototyped in #1239946-39: Embedded field collection items with a default value result in new items on save. New issue for active work in #1788222: Allow "add another" button instead of empty field collection item form when using the embedded widget

  2. Field collection shows only created collections with an "add another item" button. When no created collections exist, field collection shows an empty field collection item form and an "add another item" button. If the initial empty field collection item form has a collected field with a default value, it will need special validation to prevent it from being saved on first node save.

    Working in 1.x-dev since #1239946: Embedded field collection items with a default value result in new items on save.

  3. Field collection shows created collections with an empty field collection item form and "add another item" button. When no created collections exist, field collection shows an empty field collection item form and an "add another item" button. This is default Drupal behavior. Empty field collection item forms with default values will need special validation to prevent them from being saved on every node save.

    Special validation being worked on in: #1662998: Create configuration option to ignore chosen collected fields when checking for an empty field collection

API considerations

Based on the comments in #1662998: Create configuration option to ignore chosen collected fields when checking for an empty field collection and #1614578: Allow other modules to determine if collection item is empty, avoid saving empty item or rendering empty collection, special validation for the form items should integrate with field collection's check empty function/method, which will be the final gateway for creating a new field collection item. (Note: didn't read the code here, just the issues. Please revise with better information).

Comments

RobW’s picture

Issue summary: View changes

added api section

RobW’s picture

Issue summary: View changes

formatting

heddn’s picture

#2382855: Required field collection, all optional child fields addresses a gap where a required field_collection with all optional child fields does not require the field collection.