Issue #3561003: Scope form_alter to media edit forms and support nested edit modals
Problem
media_library_edit breaks when a media reference field with the edit
button is nested inside another one that also has it. The reported case
is a node with a media reference (episodes), where each episode is a
media entity that itself has a media reference (a thumbnail), all using
the media library widget with "Show edit" enabled. Opening the second
level "Insert selected", and editing a third level, misbehaved in
several ways.
Fixes in this MR
1. form_alter targeted the wrong forms.
media_library_edit_form_alter() ran on any form built during a
media_library_edit=ajax request, including the media library select
and add forms that the edited media's own reference field builds. That
broke "Insert selected". It now only alters the media entity edit form.
Each edit modal also gets a unique #modal_selector so nested edits
stack a new dialog instead of replacing the current one (needs core
support, see #2741877 / core MR!8105).
2. Save replaced the wrong preview.
On save the success handler replaced .selected-media, a shared marker
class the JS adds to whichever item is being edited. That selector
matched more than one preview, so saving an inner media overwrote the
outer widget's preview with the inner media's render (for example an
episode preview replaced by its thumbnail). The JS "remove old marker"
line was also a no-op (find('selected-media'), missing the .), so
stale markers were never cleared.
Fix, scoped per media id: the edit link carries
data-media-library-edit-id, the JS marks the item's preview with
js-media-library-edit-selected-<id>, and the save ReplaceCommand
targets that id, replacing only the media that was edited.
3. Validation messages leaked onto other forms.
The error branch prepended status messages to form, matching every
form on the page, so an inner edit form's errors also appeared on the
outer form(s). It now targets the form's stable data-drupal-selector.
4. Save messages leaked to the next page load.
The modal save queues a status message, but the ajax response never
rendered it, so it lingered in the session and surfaced on the next
unrelated page the user visited. Queued messages are now pushed into
the response with MessageCommand and the queue is drained, so the
confirmation shows via ajax and does not resurface later.
Compatibility with unpatched core
Fixes 2, 3 and 4 are module side only and independent of core modal stacking (#2741877 / core MR!8105). They work on stock core and also improve the non nested case (correct single item replace, messages no longer landing on a background form, no leaked save messages). The modal id changes in fix 1 degrade to a safe no-op or fallback without the core patch.
Actual multi level nesting still needs core MR!8105 so the modals stack and each gets a unique id.
Known minor edge
Nesting edits of the same bundle share one data-drupal-selector, so
validation messages would show on both the inner and outer form. Rare,
and still far better than every form.