From 7bd749f16a772d6a3de059c7900f8bf86a891f5a Mon Sep 17 00:00:00 2001
From: Lauri Eskola <lauri.eskola@acquia.com>
Date: Fri, 8 Dec 2023 10:22:45 +0200
Subject: [PATCH] Issue #2805219 by yash.rode, andrewmacpherson, seanB,
 narendraR: Some dialogs do not receive focus when opened

---
 core/misc/ajax.js                                             | 4 +++-
 .../tests/src/FunctionalJavascript/FieldBlockTest.php         | 3 +++
 .../src/Plugin/Field/FieldWidget/MediaLibraryWidget.php       | 3 ---
 .../src/Plugin/views/field/MediaLibrarySelectForm.php         | 3 ---
 4 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/core/misc/ajax.js b/core/misc/ajax.js
index 9f15b2fad4d4..bb8b46576b2c 100644
--- a/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -1085,7 +1085,9 @@
     const focusChanged = Object.keys(response || {}).some((key) => {
       const { command, method } = response[key];
       return (
-        command === 'focusFirst' || (command === 'invoke' && method === 'focus')
+        command === 'focusFirst' ||
+        command === 'openDialog' ||
+        (command === 'invoke' && method === 'focus')
       );
     });
 
diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php
index 5777d6c2fb83..c3337ff51c63 100644
--- a/core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php
+++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php
@@ -75,6 +75,9 @@ public function testUserFieldBlock() {
     $this->clickLink('Place block');
     $assert_session->assertWaitOnAjaxRequest();
 
+    // Ensure that focus is on the first focusable element on modal.
+    $this->assertJsCondition('document.activeElement === document.getElementsByClassName("block-filter-text")[0]');
+
     // Ensure that fields without any formatters are not available.
     $assert_session->pageTextNotContains('Password');
     // Ensure that non-display-configurable fields are not available.
diff --git a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
index 260e76d478a9..6ba3510eb75b 100644
--- a/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
+++ b/core/modules/media_library/src/Plugin/Field/FieldWidget/MediaLibraryWidget.php
@@ -527,9 +527,6 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
           'type' => 'throbber',
           'message' => $this->t('Opening media library.'),
         ],
-        // The AJAX system automatically moves focus to the first tabbable
-        // element of the modal, so we need to disable refocus on the button.
-        'disable-refocus' => TRUE,
       ],
       // Allow the media library to be opened even if there are form errors.
       '#limit_validation_errors' => [],
diff --git a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php
index 4f5875e1ddf7..9e179eef8fab 100644
--- a/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php
+++ b/core/modules/media_library/src/Plugin/views/field/MediaLibrarySelectForm.php
@@ -104,9 +104,6 @@ public function viewsForm(array &$form, FormStateInterface $form_state) {
         'query' => $query,
       ],
       'callback' => [static::class, 'updateWidget'],
-      // The AJAX system automatically moves focus to the first tabbable
-      // element of the modal, so we need to disable refocus on the button.
-      'disable-refocus' => TRUE,
     ];
 
     $form['actions']['submit']['#value'] = $this->t('Insert selected');
-- 
GitLab