diff --git a/core/modules/contextual/js/contextual.js b/core/modules/contextual/js/contextual.js index bbbf9ba67..adf22940c 100644 --- a/core/modules/contextual/js/contextual.js +++ b/core/modules/contextual/js/contextual.js @@ -83,6 +83,11 @@ * The server-side rendered HTML for this contextual link. */ function initContextual($contextual, html) { + if (!$contextual || !$contextual.length) { + console.warn('Skipping contextual init: element not found or empty.'); + return; + } + const $region = $contextual.closest('.contextual-region'); const { contextual } = Drupal; @@ -164,10 +169,12 @@ // the chance to set up an event listener on the collection // Drupal.contextual.collection. window.setTimeout(() => { - initContextual( - $context.find(`[data-contextual-id="${contextualID.id}"]:empty`), - html, - ); + const $contextual = $context + .find(`[data-contextual-id="${contextualID.id}"]:empty`) + .eq(0); + if ($contextual.length) { + initContextual($contextual, html); + } }); return; } @@ -351,11 +358,16 @@ this.$contextual.find('.contextual-links').prop('hidden', !isOpen); const trigger = this.$contextual.find('.trigger').get(0); - trigger.textContent = Drupal.t('@action @title configuration options', { - '@action': !isOpen ? this.strings.open : this.strings.close, - '@title': this.title, - }); - trigger.setAttribute('aria-pressed', isOpen); + if (trigger) { + trigger.textContent = Drupal.t( + '@action @title configuration options', + { + '@action': !isOpen ? this.strings.open : this.strings.close, + '@title': this.title, + }, + ); + trigger.setAttribute('aria-pressed', isOpen); + } } /**