diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php index 627b041e04..e99a0d3c2c 100644 --- a/core/lib/Drupal/Core/Render/Element/RenderElement.php +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php @@ -267,11 +267,6 @@ public static function preRenderAjaxForm($element) { return $element; } - // Add a data attribute to disable automatic refocus after ajax call. - if (!empty($element['#ajax']['disable-refocus'])) { - $element['#attributes']['data-disable-refocus'] = "true"; - } - // Add a reasonable default event handler if none was specified. if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) { switch ($element['#type']) { @@ -324,6 +319,25 @@ public static function preRenderAjaxForm($element) { } } + // Disable automatic refocus after ajax call by adding a data attribute if + // the disable-refocus flag has been set. + if (isset($element['#ajax']['event'])) { + switch ($element['#ajax']['event']) { + // Disable automatic refocus for the ajax event 'blur' on default to + // avoid an infinite focus loop. + case 'blur': + if (!isset($element['#ajax']['disable-refocus']) || $element['#ajax']['disable-refocus'] === 'true') { + $element['#attributes']['data-disable-refocus'] = "true"; + } + break; + + default: + if (isset($element['#ajax']['disable-refocus']) && $element['#ajax']['disable-refocus'] === 'true') { + $element['#attributes']['data-disable-refocus'] = "true"; + } + } + } + // Attach JavaScript settings to the element. if (isset($element['#ajax']['event'])) { $element['#attached']['library'][] = 'core/internal.jquery.form';