diff --git a/cookiebot.libraries.yml b/cookiebot.libraries.yml index ba1b7f5..0245c3c 100644 --- a/cookiebot.libraries.yml +++ b/cookiebot.libraries.yml @@ -4,4 +4,5 @@ cookiebot: dependencies: - core/jquery.cookie - core/jquery.once + - core/jquery - core/drupal diff --git a/js/cookiebot.es6.js b/js/cookiebot.es6.js index e793d88..0c2b70e 100644 --- a/js/cookiebot.es6.js +++ b/js/cookiebot.es6.js @@ -38,6 +38,25 @@ Cookiebot.renew(); }); + /** + * jQuery document ready handler. + */ + jQuery(function () { + /* + See: https://www.drupal.org/project/cookiebot/issues/3091260 + This part is only required when auto-blocking functionality is enabled + data-blockingmode="auto" (https://www.cookiebot.com/en/developer/). + If that mode is enabled, all scripts are deferred and Drupals + attachBehaviors is not executed in any element, Cookiebots + auto-blocking relies on jQuery + https://www.lullabot.com/articles/understanding-javascript-behaviors-in-drupal + */ + if (typeof Drupal.cookiebot !== 'undefined' && !Drupal.cookiebot.attachedBehaviorsInAutoBlockingmode) { + Drupal.attachBehaviors(document, Drupal.settings); + Drupal.cookiebot.attachedBehaviorsInAutoBlockingmode = true; + } + }); + /** * Listens to the event of a user declining the use of cookies. * @@ -52,7 +71,7 @@ */ Drupal.behaviors.cookiebot = { attach: function attach(context) { - Drupal.cookiebot.updateCookies(); + Drupal.cookiebot.updateCookies(context); $('.cookiebot-renew', context).once().on('click', (event) => { event.preventDefault(); @@ -69,7 +88,8 @@ * part of the public API. */ Drupal.cookiebot = { - updateCookies() { + attachedBehaviorsInAutoBlockingmode: false, + updateCookies(context) { const cookieNames = [ 'necessary', 'preferences', @@ -77,6 +97,11 @@ 'marketing', ]; + // Set global variable to only execute once on page load + if (context === document) { + Drupal.cookiebot.attachedBehaviorsInAutoBlockingmode = true; + } + if (typeof Cookiebot === 'undefined' || Cookiebot.consent === void (0)) { return; } diff --git a/js/cookiebot.js b/js/cookiebot.js index ce4d267..af4ae68 100644 --- a/js/cookiebot.js +++ b/js/cookiebot.js @@ -53,6 +53,29 @@ Cookiebot.renew(); }); + /** + * jQuery document ready handler. + */ + jQuery(function () { + /* + See: https://www.drupal.org/project/cookiebot/issues/3091260 + + This part is only required when auto-blocking functionality is enabled + data-blockingmode="auto" (https://www.cookiebot.com/en/developer/). + + If that mode is enabled, all scripts are deferred and Drupals + attachBehaviors is not executed in any element, Cookiebots + auto-blocking relies on jQuery + https://www.lullabot.com/articles/understanding-javascript-behaviors-in-drupal + */ + if(typeof Drupal.cookiebot !== 'undefined' && !Drupal.cookiebot.attachedBehaviorsInAutoBlockingmode){ + Drupal.attachBehaviors(document, Drupal.settings); + // Set global variable to only execute once on page load + Drupal.cookiebot.attachedBehaviorsInAutoBlockingmode = true; + } + }); + + /** * Attach Cookiebot renew click event listener. * @@ -60,7 +83,7 @@ */ Drupal.behaviors.cookiebot = { attach: function attach(context) { - Drupal.cookiebot.updateCookies(); + Drupal.cookiebot.updateCookies(context); $('.cookiebot-renew', context).once().on('click', function (event) { event.preventDefault(); @@ -77,7 +100,8 @@ * part of the public API. */ Drupal.cookiebot = { - updateCookies: function updateCookies() { + attachedBehaviorsInAutoBlockingmode: false, + updateCookies: function updateCookies(context) { var cookieNames = [ 'necessary', 'preferences', @@ -85,6 +109,11 @@ 'marketing' ]; + // Set global variable to only execute once on page load + if (context === document) { + Drupal.cookiebot.attachedBehaviorsInAutoBlockingmode = true; + } + if (typeof Cookiebot === 'undefined' || Cookiebot.consent === void (0)) { return; }