Commit bb9f855d authored by Sven Berg Ryen's avatar Sven Berg Ryen Committed by Sven Berg Ryen
Browse files

Issue #3029549 by svenryen, caco_d25, gapple, jimconte, Anybody: Compatibility...

Issue #3029549 by svenryen, caco_d25, gapple, jimconte, Anybody: Compatibility with Content Security Policy
parent 896d5ddb
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -677,3 +677,15 @@ function eu_cookie_compliance_update_8126() {
    ->set('popup_info_template', 'new')
    ->save();
}

/**
 * Trigger building a JavaScript file (see https://www.drupal.org/node/3029549).
 */
function eu_cookie_compliance_update_8127() {
  // Clear the cache so we can see the new EventSubscriber.
  drupal_flush_all_caches();
  // Save config to trigger rebuilding the JavaScript file.
  \Drupal::configFactory()
    ->getEditable('eu_cookie_compliance.settings')
    ->save();
}
+21 −39
Original line number Diff line number Diff line
@@ -176,28 +176,41 @@ function eu_cookie_compliance_page_attachments(&$variables) {
  $variables['#attached']['drupalSettings']['eu_cookie_compliance']['hide_the_banner'] = $hide_the_banner;
  $variables['#attached']['library'][] = 'eu_cookie_compliance/eu_cookie_compliance_' . ($config->get('use_bare_css') ? 'bare' : 'default');

  // Add javascript.
  $uri = 'public://eu_cookie_compliance/eu_cookie_compliance.script.js';
  if (\Drupal::VERSION < 9.3) {
    $url = file_url_transform_relative(file_create_url($uri));
  }
  else {
    $url = \Drupal::service('file_url_generator')->generateString($uri);
  }
  $query_string = \Drupal::state()->get('system.css_js_query_string') ?: '0';
  $weight = 9;

  $variables['#attached']['html_head'][] = [
    [
      '#type' => 'html_tag',
      '#tag' => 'script',
      '#attributes' => ['src' => $url . '?' . $query_string, 'defer' => TRUE],
      '#weight' => $weight,
    ],
    'eu_cookie_compliance_script_tag__eu_cookie_compliance',
  ];

  // Add inline javascript.
  $disabled_javascripts = $config->get('disabled_javascripts');
  $load_disabled_scripts = '';
  // Initialize a variable to keep libraries to we wish to disable.
  $libraries_to_disable = [];
  if ($disabled_javascripts != '') {
    $load_disabled_scripts = '';
    $disabled_javascripts = _eu_cookie_compliance_explode_multiple_lines($disabled_javascripts);
    $disabled_javascripts = array_filter($disabled_javascripts, 'strlen');

    foreach ($disabled_javascripts as $key => $script) {
      $parts = explode('%3A', $script);
      $category = NULL;
      if (count($parts) > 1) {
        $category = array_shift($parts);
        $script = implode(':', $parts);
      }

      // Split the string if a | is present.
      // The second parameter (after the |) will be used to trigger a script
      // attach.
      $attach_name = '';
      if (strpos($script, '%7C') !== FALSE) {
        // Swallow a notice in case there are no behavior or library names.
        @list($script, $attach_name, $library_name) = explode('%7C', $script);
@@ -208,41 +221,10 @@ function eu_cookie_compliance_page_attachments(&$variables) {
        }
      }

      _eu_cookie_compliance_convert_relative_uri($script);

      if (strpos($script, 'http') !== 0 && strpos($script, '//') !== 0) {
        $script = '/' . $script;
      }

      // Store the actual script name, since we will need it later.
      $disabled_javascripts[$key] = $script;

      if ($category) {
        $load_disabled_scripts .= 'if (category === "' . $category . '") {';
      }
      $load_disabled_scripts .= 'var scriptTag = document.createElement("script");';
      $load_disabled_scripts .= 'scriptTag.src = ' . Json::encode($script) . ';';
      $load_disabled_scripts .= 'document.body.appendChild(scriptTag);';
      // The script will not immediately load, so we need to trigger the
      // attach in an interval function.
      if ($attach_name) {
        $load_disabled_scripts .= 'var EUCookieInterval' . $attach_name . '= setInterval(function() { if (Drupal.behaviors.' . $attach_name . ' !== undefined) { Drupal.behaviors.' . $attach_name . '.attach(document, drupalSettings);clearInterval(EUCookieInterval' . $attach_name . ')};}, 100);';
      }
      if ($category) {
        $load_disabled_scripts .= '}';
      }
    }
    }

  if ($load_disabled_scripts) {
    $variables['#attached']['html_head'][] = [
      [
        '#type' => 'html_tag',
        '#tag' => 'script',
        '#value' => 'window.euCookieComplianceLoadScripts = function(category) {' . $load_disabled_scripts . '}',
      ],
      'eu-cookie-compliance-js',
    ];
  }

  // Add inline css.
+4 −0
Original line number Diff line number Diff line
@@ -2,6 +2,10 @@ services:
  plugin.manager.eu_cookie_compliance.consent_storage:
    class: Drupal\eu_cookie_compliance\Plugin\ConsentStorageManager
    parent: default_plugin_manager
  eu_cookie_compliance.config_subscriber:
    class: Drupal\eu_cookie_compliance\EventSubscriber\EuCookieComplianceConfigEventsSubscriber
    tags:
      - { name: event_subscriber }
  eu_cookie_compliance.clear_cache:
    class: Drupal\eu_cookie_compliance\Plugin\EuCcClearCache
    arguments: ['@module_handler', '@language_manager', '@entity_type.manager', '@extension.list.theme', '@cache.render']