Problem/Motivation
When editing a node with a Media Library Image, entering the edit form will jump the user to the last focused element. A good amount of the time this will be the bottom element, which looks strange for an editor's ui.
Expected Behavior

Current Behavior

Proposed resolution
Limit the Ajax focus functionality, so when the editor clicks edit they remain at the top of the edit form.
The Limit the Ajax focus functionality was possibly introduced in Improve refocus on submit buttons of Media Library Widget modals @ https://www.drupal.org/project/drupal/issues/3016807
Steps to Reproduce
Add a media entity reference field to a content type.
Add additional fields to space things out (eg. numbers, links, textfields, booleans, etc.).
Create a new piece of content of the content type filling out all the fields except adding the image.
Save, then go back to the edit form.
Observe being at the top.
Now add an image.
Save, then edit the content.
Observe jumping to the image.
Edit one of the other fields and save.
Edit the content.
Observe jumping to (or around) the previously edited field.
I ran these test on https://simplytest.me using Drupal 8.7.5. And only enabled media and media library, plus the defaults.
| Comment | File | Size | Author |
|---|---|---|---|
| #16 | 3073023-16.patch | 1.96 KB | lendude |
| #16 | interdiff-3073023-13-16.txt | 948 bytes | lendude |
| #13 | 3073023-13.patch | 1.87 KB | idebr |
Comments
Comment #2
oheller commentedHere's a proof of concept. I've not been able to create a patch using git that would apply via composer.
Comment #3
wim leersGreat find!
Comment #4
gaurav.kapoor commentedI have modified the patch so that it can be applied using composer. Had a similar issue in one of the projects and this worked fine for us. Not sure if this is the right way to solve this issue.
Comment #5
xpersonas commentedI can't get that patch to apply after updating to 8.8. Here's my patch. Not doing anything different.
Comment #7
xpersonas commentedWell, my patch failed testing. But I don't know enough about the Media Library code to make an educated guess about that assertion - whether I've broken something needed or the assertion needs altered.
Everything seems to work though.
Comment #8
seanbAlso posted this in #3089745: Add focus behaviour for media widget with max elements, not sure which issue to close as a duplicate, both issues have valuable patches. Anyway, my prefered solution would be to conditionally add the
data-disabled-focusattribute.In
MediaLibraryWidgetwe have this:We might want to check
$form_state->getTriggeringElement()before we add thedata-disabled-focusattribute. We only want to trigger the refocus when a user selected something in the library. The triggering element should be themedia_library_update_widgetbutton.I think this might be the best way to fix this.
Comment #9
swatichouhan012 commentedI am working on this.
Comment #10
swatichouhan012 commentedI added new patch according comment #8, Kindly review.
Comment #11
swatichouhan012 commentedComment #13
idebr commented#10 The changes to the JavaScript files are redundant: the 'Add media' button should only be focused programmatically when returning from the media library modal, but the button should also be visually-hidden when no more items can be added.
Comment #16
lendudeThis raised notices because triggering element can be NULL.
Tried to debug the test fail, but the test fails much sooner locally then it does on the bot with what seem like unrelated fails ¯\_(ツ)_/¯
Comment #18
dan_metille commentedNow neither patch #13 nor #16 apply with Drupal 8.9.3
Comment #19
papagrandeLooks like this was fixed in https://www.drupal.org/project/drupal/issues/3089745. Based on my manual testing, this is fixed.
I'm closing this issue as a duplicate.
Comment #20
dan_metille commentedGreat! Really Great!!