fix: #3620147 Widget does not preselect stored values on edit forms since...

fix: #3620147 Widget does not preselect stored values on edit forms since 2.0.4 (regression from #3517342) Builds on !58 (do not close it — this includes its commits).

!58 fixes the reported bug, but its pipeline fails testEditNodeWithDeletedTerm, added in #3359851. Returning stored values unfiltered re-exposes that bug: when a referenced term has been deleted, the stale ID is preselected and the reference constraint rejects the save with "The referenced entity (taxonomy_term: X) does not exist." The entity then can't be saved at all, and there's no way to clear it through the UI — worse than the empty widget.

This drops values whose target no longer exists, using a single entity query rather than loading each referenced entity, so the memory saving from #3517342 is preserved.

Credit to @carlitus for the original diagnosis and test, and @malcomio for !58.

Verified on Drupal 11.4.8 / shs 2.0.7 across 8 form displays (5 node types, a paragraph type, menu links): valid terms preselect, stale references drop, mixed live/dead preserves order, and a node holding a stale reference saves and clears.

Note: \Drupal:: statics match the class's existing style (formElement() already uses them). Injecting entity_type.manager would change the constructor signature, which is a BC concern for subclasses — happy to switch if you prefer.

Closes #3620147

Edited by Sahal V A

Merge request reports

Loading
Loading