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

No Media Library Image, no jump

Current Behavior

Media Library Image, jump

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.

Comments

oheller created an issue. See original summary.

oheller’s picture

Here's a proof of concept. I've not been able to create a patch using git that would apply via composer.

wim leers’s picture

Issue tags: +Usability, +Accessibility

Great find!

gaurav.kapoor’s picture

Version: 8.7.5 » 8.8.x-dev
Status: Active » Needs review
StatusFileSize
new1.69 KB

I 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.

xpersonas’s picture

StatusFileSize
new2.06 KB

I can't get that patch to apply after updating to 8.8. Here's my patch. Not doing anything different.

Status: Needs review » Needs work

The last submitted patch, 5: 3073023-5.patch, failed testing. View results

xpersonas’s picture

Well, 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.

    $open_button = $this->assertElementExistsAfterWait('css', '.js-media-library-open-button[name^="field_twin_media"]');
    $this->assertTrue($open_button->hasAttribute('data-disabled-focus'));
    $this->assertTrue($open_button->hasAttribute('disabled'));

Everything seems to work though.

seanb’s picture

Also 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-focus attribute.

In MediaLibraryWidget we have this:

    // When the user returns from the modal to the widget, we want to shift the
    // focus back to the open button. If the user is not allowed to add more
    // items, the button needs to be disabled. Since we can't shift the focus to
    // disabled elements, the focus is set back to the open button via
    // JavaScript by adding the 'data-disabled-focus' attribute.
    // @see Drupal.behaviors.MediaLibraryWidgetDisableButton
    if (!$cardinality_unlimited && $remaining === 0) {
      $element['open_button']['#attributes']['data-disabled-focus'] = 'true';
      $element['open_button']['#attributes']['class'][] = 'visually-hidden';
    }

We might want to check $form_state->getTriggeringElement() before we add the data-disabled-focus attribute. We only want to trigger the refocus when a user selected something in the library. The triggering element should be the media_library_update_widget button.

I think this might be the best way to fix this.

swatichouhan012’s picture

Assigned: Unassigned » swatichouhan012

I am working on this.

swatichouhan012’s picture

Status: Needs work » Needs review
StatusFileSize
new3.07 KB
new855 bytes

I added new patch according comment #8, Kindly review.

swatichouhan012’s picture

Assigned: swatichouhan012 » Unassigned

Status: Needs review » Needs work

The last submitted patch, 10: 3073023-10.patch, failed testing. View results

idebr’s picture

Version: 8.8.x-dev » 8.9.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.87 KB

#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.

Status: Needs review » Needs work

The last submitted patch, 13: 3073023-13.patch, failed testing. View results

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

lendude’s picture

Status: Needs work » Needs review
StatusFileSize
new948 bytes
new1.96 KB

This 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 ¯\_(ツ)_/¯

Status: Needs review » Needs work

The last submitted patch, 16: 3073023-16.patch, failed testing. View results

dan_metille’s picture

Now neither patch #13 nor #16 apply with Drupal 8.9.3

papagrande’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#3089745: Add focus behaviour for media widget with max elements

Looks 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.

dan_metille’s picture

Great! Really Great!!