Problem/Motivation

After an IEF form is cancelled or an item is removed the inline_entity_form storage is not cleared properly for that field.

If entity reference fields are used inside the edited entity then previously filled in entity reference fields will show inside the IEF entity, even though we are creating a new entity,

Steps to reproduce

  1. Standard Drupal installation
  2. Create an entity reference field with ief widget inside article that allows both basic pages and articles
  3. Create an article with an article inside with an article inside (root level + 2 levels deep)
  4. Cancel the first level IEF widget
  5. Create a new entity
  6. This new entity will reference the entity reference from the previously canceled article

The same behavior occurs when deleting the inline entity.

I've tested this with bundle selection.

Proposed resolution

On 'cancel' and 'delete' actions, the form state storage for that field and all underlying ief fields should be cleared.

Remaining tasks

Tests. Find out where this should be fixed.

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

nuez created an issue. See original summary.

nuez’s picture

Status: Active » Needs review

benstallings’s picture

Version: 3.0.0-rc19 » 3.x-dev

Hi, @nuez. I'm going to make another MR with a different approach:

  • Introduces one module-level helper, inline_entity_form_purge_nested_widget_state($form_state, $prefix), that unsets every inline_entity_form storage key beginning with "$prefix-". The trailing - is appended by the helper so outer-1 cannot over-match outer-10…. Uses strncmp() so PHP 7.1 compatibility is preserved.
  • Calls the helper from the two submit handlers that already hold the authoritative $ief_id and $delta: InlineEntityFormComplex::submitCloseRow() (the edit-cancel and remove-cancel path) and InlineEntityFormComplex::submitConfirmRemove() (the confirmed-removal path). No button-name reverse-engineering; no render-time mutation; inline_entity_form_get_element() stays read-only.
  • Keeps the two small defensive null-coalesce fixes from your branch (inline_entity_form_close_all_forms tolerating a missing entities substate; submitConfirmRemove tolerating a missing delete value) as a separate first commit so they can be cherry-picked independently.
  • Adds a Unit test (tests/src/Unit/NestedWidgetStatePurgeTest.php) that locks in the helper's behavior: descendants are purged, the prefix key itself is preserved, sibling-row and unrelated keys are untouched, an empty storage root is a no-op, and outer-1 does not over-match outer-10….