The function EntityInlineEntityFormController::cleanupFieldFormState() causes multivalue rows to disappear in modules that make use of items_count. Two examples are the date and field_collection modules. When cleanupFieldFormState is executed after save, it sets items_count in the field's state to zero. This results in missing items if an entity's row is reopened for additional editing after an initial save.

I think this is the easiest way to reproduce the bug:

  • Install the date module.
  • Create a node type called "Date."
  • Add a date field, and set the number of values to unlimited.
  • Create another node type called "Date Collection."
  • Add an entity reference field:
    • Use the inline entity widget.
    • Reference the "Date" content type you made earlier.
    • Set number of values to unlimited.
  • Create a new "Date Collection."
  • Using ief, add a date node. Enter 2 dates and save.
  • Reopen the row you just created, enter a 3rd date, and save.
  • Reopen the row again. Instead of having 3 rows, you only have 1.
  • Press "Add another item" a couple times and watch the 2 missing rows get resurrected.

You can also get strange results when hitting Cancel. Try this:

  • Add a new row with just one date. Save it
  • Reopen the row, and add 2 new dates, but hit Cancel this time.
  • Reopen the row, and see 2 empty ghost-dates.

Similar problems occur with field_collections configured with an unlimited number of values.

Comments

rylowry@gmail.com’s picture

Title: cleanupFieldFormState causes multivalue rows to disappear » cleanupFieldFormState causes multivalue field rows to disappear

Updated the title to make it a little more descriptive.

rylowry@gmail.com’s picture

StatusFileSize
new2.82 KB

Here is a patch that attempts to fix this problem. It seems that setting items_count to zero is not a sufficient cleanup. For example, with a field_collections field state also has an array of entities. This patch causes *all* field state for the entity reference's delta to be removed from form state when a row is closed. It was moved to happen each time a row is closed, so cleanup occurs after save and after a cancel. The cleanup after cancel takes care of the situation where you Add Another Item, then cancel out of the form without saving.

bojanz’s picture

With your patch applied, I can add a product variation, upload an image, hit save, then go to add another product variation, and in the empty form see the image from the previously saved variation, right? That's the bug this code was written to prevent.

rylowry@gmail.com’s picture

StatusFileSize
new968 bytes

Ah, it looks like I assumed too much. I was thinking that inline_entity_form_close_row_form was also called after the initial save, which was incorrect. I was to wrapped up in testing with existing rows. This patch preserves the cleanup after save, and adds some additional cleanup in inline_entity_form_close_row_form.

rylowry@gmail.com’s picture

StatusFileSize
new788 bytes

Same patch as above, but hopefully made in the correct manner.

bojanz’s picture

Status: Active » Needs review
bojanz’s picture

Issue summary: View changes

Added a note about field_collections onto the end.

bojanz’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev
Issue summary: View changes
Status: Needs review » Fixed

So, two different bugs here:
- the field form state is not properly reset, leading to problems with link, field_collection, etc.
- the cleanup doesn't run on cancellation.

Committed: http://drupalcode.org/project/inline_entity_form.git/commitdiff/0640469?...

- Made the field form state properly reset.
- cleanupFieldFormState should not be a method anymore, moved it to a helper function.
- made it called after a successful entity submit in the process form callback (so that it runs for both IEF widgets).
- made it called when both kinds of forms (add form / edit form) are cancelled.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.