Skip to content

Commit

Permalink
Merge pull request #1083 from City-of-Helsinki/UHF-8650
Browse files Browse the repository at this point in the history
UHF-8650 Preparation for the HDS cookie banner module
  • Loading branch information
khalima authored Oct 30, 2024
2 parents 3181ef4 + f8fe3ca commit 2409e1c
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 81 deletions.
2 changes: 1 addition & 1 deletion dist/js/embedded-content-cookie-compliance.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/health-station-search.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/maternity-and-child-health-clinic-search.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/js/matomo.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/school-search.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions hdbt.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ matomo:
header: true
js:
dist/js/matomo.min.js: {
weight: -10,
minified: true
}
dependencies:
Expand Down
65 changes: 36 additions & 29 deletions hdbt.theme
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,15 @@ function hdbt_preprocess_field(&$variables): void {
case 'service_map_embed':
$url_parts = parse_url($variables['items'][0]['content']['iframe']['#attributes']['src']);
$variables['map_service_url'] = $url_parts['scheme'] . "://" . $url_parts['host'];
$variables['privacy_policy_url'] = helfi_eu_cookie_compliance_get_privacy_policy_url();

if (Drupal::moduleHandler()->moduleExists('hdbt_cookie_banner')) {
$cookie_settings = Drupal::service('hdbt_cookie_banner.cookie_settings');
$variables['privacy_policy_url'] = $cookie_settings->getCookieSettingsPageUrl();
}
// @todo UHF-10862 Remove once the HDBT cookie banner module is in use.
elseif (Drupal::moduleHandler()->moduleExists('helfi_eu_cookie_compliance')) {
$variables['privacy_policy_url'] = helfi_eu_cookie_compliance_get_privacy_policy_url();
}
break;
}
}
Expand Down Expand Up @@ -1719,41 +1727,35 @@ function hdbt_preprocess_paragraph__event_list(&$variables): void {
*/
function hdbt_preprocess_paragraph__school_search(array &$variables): void {
$variables['#attached']['library'][] = 'hdbt/school-search';

$privacyUrl = helfi_eu_cookie_compliance_get_privacy_policy_url();

if ($privacyUrl instanceof Url) {
$privacyUrl->setAbsolute();
$variables['#attached']['drupalSettings']['helfi_react_search']['cookie_privacy_url'] = $privacyUrl->toString();
}
// @todo UHF-10862 Remove once the HDBT cookie banner module is in use.
hdbt_set_legacy_privacy_policy_url($variables);
}

/**
* Implements hook_preprocess_HOOK().
*/
function hdbt_preprocess_paragraph__health_station_search(array &$variables): void {
$variables['#attached']['library'][] = 'hdbt/health-station-search';

$privacyUrl = helfi_eu_cookie_compliance_get_privacy_policy_url();

if ($privacyUrl instanceof Url) {
$privacyUrl->setAbsolute();
$variables['#attached']['drupalSettings']['helfi_react_search']['cookie_privacy_url'] = $privacyUrl->toString();
}
// @todo UHF-10862 Remove once the HDBT cookie banner module is in use.
hdbt_set_legacy_privacy_policy_url($variables);
}

/**
* Implements hook_preprocess_HOOK().
*/
function hdbt_preprocess_paragraph__maternity_and_child_health_clini(array &$variables): void {
$variables['#attached']['library'][] = 'hdbt/maternity-and-child-health-clinic-search';
// @todo UHF-10862 Remove once the HDBT cookie banner module is in use.
hdbt_set_legacy_privacy_policy_url($variables);
}

$privacyUrl = helfi_eu_cookie_compliance_get_privacy_policy_url();

if ($privacyUrl instanceof Url) {
$privacyUrl->setAbsolute();
$variables['#attached']['drupalSettings']['helfi_react_search']['cookie_privacy_url'] = $privacyUrl->toString();
}
/**
* Implements hook_preprocess_HOOK().
*/
function hdbt_preprocess_paragraph__ploughing_schedule(array &$variables): void {
$variables['#attached']['library'][] = 'hdbt/ploughing-schedule';
// @todo UHF-10862 Remove once the HDBT cookie banner module is in use.
hdbt_set_legacy_privacy_policy_url($variables);
}

/**
Expand Down Expand Up @@ -1888,15 +1890,20 @@ function hdbt_preprocess_views_exposed_form(array &$variables) {
}

/**
* Implements hook_preprocess_HOOK().
* Get privacy policy url from helfi_eu_cookie_compliance module.
*
* @todo UHF-10862 Remove once the HDBT cookie banner module is in use.
*
* @param array $variables
* Variables array.
*/
function hdbt_preprocess_paragraph__ploughing_schedule(array &$variables): void {
$variables['#attached']['library'][] = 'hdbt/ploughing-schedule';

$privacyUrl = helfi_eu_cookie_compliance_get_privacy_policy_url();
function hdbt_set_legacy_privacy_policy_url(array &$variables): void {
if (Drupal::moduleHandler()->moduleExists('helfi_eu_cookie_compliance')) {
$privacyUrl = helfi_eu_cookie_compliance_get_privacy_policy_url();

if ($privacyUrl instanceof Url) {
$privacyUrl->setAbsolute();
$variables['#attached']['drupalSettings']['helfi_react_search']['cookie_privacy_url'] = $privacyUrl->toString();
if ($privacyUrl instanceof Url) {
$privacyUrl->setAbsolute();
$variables['#attached']['drupalSettings']['helfi_react_search']['cookie_privacy_url'] = $privacyUrl->toString();
}
}
}
30 changes: 8 additions & 22 deletions src/js/embedded-content-cookie-compliance.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
/**
* @file
* Load embedded content once the user has approved required cookie category.
*
* Depends on EU Cookie Compliance module.
*/
// eslint-disable-next-line func-names
(function ($, Drupal, drupalSettings) {
function loadEmbeddedContent() {
if (
typeof Drupal.eu_cookie_compliance === 'undefined' ||
!drupalSettings.embedded_media_attributes
) {
return;
}

if (
Drupal.eu_cookie_compliance.hasAgreed('preference') &&
Drupal.eu_cookie_compliance.hasAgreed('statistics')
) {
const loadEmbeddedContent = () => {
if (Drupal.cookieConsent.getConsentStatus(['preferences', 'statistics'])) {
// eslint-disable-next-line no-restricted-syntax
for (const [id, attributes] of Object.entries(drupalSettings.embedded_media_attributes)) {
const iframeElement = document.createElement('iframe');
Expand Down Expand Up @@ -68,18 +57,15 @@
.replaceWith(skipLinkBefore, containerElement, skipLinkAfter);
}
}

// Only load the embedded content once.
$(document).off('eu_cookie_compliance.changeStatus', loadEmbeddedContent);
}
}

// Run after choosing cookie settings.
$(document).on('eu_cookie_compliance.changeStatus', loadEmbeddedContent);
};

// Remove noscript element.
$('.embedded-content-cookie-compliance .js-remove').remove();

// Run after page is ready.
$(document).ready(loadEmbeddedContent);
if (Drupal.cookieConsent.initialized()) {
loadEmbeddedContent();
} else {
Drupal.cookieConsent.loadFunction(loadEmbeddedContent);
}
})(jQuery, Drupal, drupalSettings);
20 changes: 8 additions & 12 deletions src/js/matomo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// eslint-disable-next-line func-names
(function ($, Drupal) {
function loadMatomoAnalytics() {
/**
* If the queryparameter is found, the script will be loaded
/**
* If the queryparameter is found, the script will be loaded
* regardless of cookie consents etc.
*/
const useJSAPI = window.location.search === '?9mt5bfb2bGk=';
Expand Down Expand Up @@ -40,12 +40,8 @@
})();
}

if (!useJSAPI && typeof Drupal.eu_cookie_compliance === 'undefined') {
return;
}

// Load Matomo only if statistics cookies are allowed.
if (!useJSAPI && Drupal.eu_cookie_compliance.hasAgreed('statistics')) {
if (!useJSAPI && Drupal.cookieConsent.getConsentStatus(['statistics'])) {
// Matomo Tag Manager
// eslint-disable-next-line no-multi-assign
const _mtm = (window._mtm = window._mtm || []);
Expand All @@ -63,11 +59,11 @@
}
}

// Load when cookie settings are changed.
$(document).on('eu_cookie_compliance.changeStatus', loadMatomoAnalytics());

// Load on page load.
$(document).ready(loadMatomoAnalytics);
if (Drupal.cookieConsent.initialized()) {
loadMatomoAnalytics();
} else {
Drupal.cookieConsent.loadFunction(loadMatomoAnalytics);
}
})(jQuery, Drupal);

// Clean/refactor this file when the testing phase is done
Expand Down
Loading

0 comments on commit 2409e1c

Please sign in to comment.