diff --git a/core/misc/dialog/dialog.ajax.es6.js b/core/misc/dialog/dialog.ajax.es6.js index 419d0ea58c..7d3452fd7a 100644 --- a/core/misc/dialog/dialog.ajax.es6.js +++ b/core/misc/dialog/dialog.ajax.es6.js @@ -132,7 +132,7 @@ response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog); } - if (ajax.hasOwnProperty('element')) { + if (ajax.hasOwnProperty('element') && ajax.element instanceof Element) { response.dialogOptions.drupalTriggerElement = ajax.element; } diff --git a/core/misc/dialog/dialog.ajax.js b/core/misc/dialog/dialog.ajax.js index ba9659f65c..53130377a7 100644 --- a/core/misc/dialog/dialog.ajax.js +++ b/core/misc/dialog/dialog.ajax.js @@ -85,7 +85,7 @@ response.dialogOptions.buttons = Drupal.behaviors.dialog.prepareDialogButtons($dialog); } - if (ajax.hasOwnProperty('element')) { + if (ajax.hasOwnProperty('element') && ajax.element instanceof Element) { response.dialogOptions.drupalTriggerElement = ajax.element; } diff --git a/core/modules/contextual/js/contextual.es6.js b/core/modules/contextual/js/contextual.es6.js index b3b5f30774..705411ef2b 100644 --- a/core/modules/contextual/js/contextual.es6.js +++ b/core/modules/contextual/js/contextual.es6.js @@ -4,7 +4,6 @@ */ (function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) { - let drupalTriggerElement; const options = $.extend(drupalSettings.contextual, // Merge strings on top of drupalSettings so that they are not mutable. { @@ -265,22 +264,28 @@ Drupal.ajax.bindAjaxLinks(data.$el[0]); }); + // The contextual button for the link that opened the dialog. + let dialogLinkContextualButton; $(window).on({ 'dialog:beforecreate': (event, dialog, $element, settings) => { if (settings.hasOwnProperty('drupalTriggerElement')) { - // Save trigger element so it will be available for 'dialog:afterclose' - // event. - drupalTriggerElement = settings.drupalTriggerElement; + // Determine if the trigger element is a contextual link. + if ($(settings.drupalTriggerElement).closest('[data-contextual-id]').find('button').length === 1) { + /** + * Save the contextual link button so it will be available for + * 'dialog:afterclose'. + */ + dialogLinkContextualButton = $(settings.drupalTriggerElement) + .closest('[data-contextual-id]') + .find('button') + .get(0); + } } }, 'dialog:afterclose': (event, dialog, $element) => { - if (drupalTriggerElement) { + if (dialogLinkContextualButton) { // Set focus to the contextual trigger button. - $(drupalTriggerElement) - .closest('[data-contextual-id]') - .find('button') - .get(0) - .focus(); + $(dialogLinkContextualButton).focus(); } }, }); diff --git a/core/modules/contextual/js/contextual.js b/core/modules/contextual/js/contextual.js index 57dfd6a47b..79e505e4c5 100644 --- a/core/modules/contextual/js/contextual.js +++ b/core/modules/contextual/js/contextual.js @@ -6,7 +6,6 @@ **/ (function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) { - var drupalTriggerElement = void 0; var options = $.extend(drupalSettings.contextual, { strings: { open: Drupal.t('Open'), @@ -150,16 +149,19 @@ Drupal.ajax.bindAjaxLinks(data.$el[0]); }); + var dialogLinkContextualButton = void 0; $(window).on({ 'dialog:beforecreate': function dialogBeforecreate(event, dialog, $element, settings) { if (settings.hasOwnProperty('drupalTriggerElement')) { - drupalTriggerElement = settings.drupalTriggerElement; + if ($(settings.drupalTriggerElement).closest('[data-contextual-id]').find('button').length === 1) { + dialogLinkContextualButton = $(settings.drupalTriggerElement).closest('[data-contextual-id]').find('button').get(0); + } } }, 'dialog:afterclose': function dialogAfterclose(event, dialog, $element) { - if (drupalTriggerElement) { - $(drupalTriggerElement).closest('[data-contextual-id]').find('button').get(0).focus(); + if (dialogLinkContextualButton) { + $(dialogLinkContextualButton).focus(); } } }); -})(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage); \ No newline at end of file +})(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage);