You are here

function cookiebot_page_attachments_alter in Cookiebot - Cookie consent, Cookie monitoring and Cookie control 8

Implements hook_page_attachments_alter().

File

./cookiebot.module, line 59
The module file.

Code

function cookiebot_page_attachments_alter(array &$page) {

  // Check hide if user can use site without giving Cookiebot cookie consent.
  if (\Drupal::currentUser()
    ->id() !== 1 && \Drupal::currentUser()
    ->hasPermission('skip cookiebot consent')) {
    return;
  }
  $config = \Drupal::config('cookiebot.settings');

  // Check hide cookie for the superuser.
  if (\Drupal::currentUser()
    ->id() == 1 && !empty($config
    ->get('exclude_uid_1')) && $config
    ->get('exclude_uid_1')) {
    return;
  }

  // Check exclude paths.
  if (!empty($config
    ->get('exclude_paths'))) {

    // Check both the URL path and the URL alias against the list to exclude.
    $path = \Drupal::service('path.current')
      ->getPath();
    $url_alias_path = \Drupal::service('path_alias.manager')
      ->getAliasByPath($path);
    $path_match = \Drupal::service('path.matcher')
      ->matchPath($path, $config
      ->get('exclude_paths'));
    $path_match_url_alias = \Drupal::service('path.matcher')
      ->matchPath($url_alias_path, $config
      ->get('exclude_paths'));
    $path_match = $path_match || $path_match_url_alias;
    $exclude_paths = $config
      ->get('exclude_paths');
    \Drupal::moduleHandler()
      ->alter('cookiebot_path_match', $path_match, $path, $exclude_paths);
    if ($path_match) {
      return;
    }
  }

  // Check hide cookie compliance on admin theme.
  if ($config
    ->get('exclude_admin_theme')) {

    // Determines whether the active route is an admin one.
    $is_route_admin = \Drupal::service('router.admin_context')
      ->isAdminRoute();
    if ($is_route_admin) {
      return;
    }
  }
  $cbid = $config
    ->get('cookiebot_cbid');
  $page['#cache']['tags'] = Cache::mergeTags(!empty($page['#cache']['tags']) ? $page['#cache']['tags'] : [], [
    'cookiebot:cbid',
    'cookiebot:iab_enabled',
  ]);
  if (empty($cbid)) {
    return;
  }
  $cookiebot = [
    '#type' => 'html_tag',
    '#tag' => 'script',
    '#attributes' => [
      'type' => 'text/javascript',
      'id' => 'Cookiebot',
      'src' => 'https://consent.cookiebot.com/uc.js',
      'data-cbid' => $cbid,
    ],
  ];
  if ($config
    ->get('cookiebot_block_cookies')) {
    $cookiebot['#attributes']['data-blockingmode'] = 'auto';
  }
  else {
    $cookiebot['#attributes']['async'] = 'async';
  }
  $iab = $config
    ->get('cookiebot_iab_enabled');
  if ($iab) {
    $cookiebot['#attributes']['data-framework'] = 'IAB';
  }

  // Attach the Cookiebot library.
  $page['#attached']['html_head'][] = [
    $cookiebot,
    'cookiebot',
  ];
  $message_placeholder_cookieconsent_optout_marketing_build = [
    '#theme' => 'cookiebot_blocked_element_placeholder',
    '#content' => [
      '#type' => 'processed_text',
      '#text' => $config
        ->get('message_placeholder_cookieconsent_optout_marketing.value'),
      '#format' => $config
        ->get('message_placeholder_cookieconsent_optout_marketing.format'),
    ],
  ];
  $page['#attached']['drupalSettings']['cookiebot'] = [
    'message_placeholder_cookieconsent_optout_marketing_show' => $config
      ->get('message_placeholder_cookieconsent_optout_marketing_show') ? TRUE : FALSE,
    'message_placeholder_cookieconsent_optout_marketing' => \Drupal::service('renderer')
      ->renderRoot($message_placeholder_cookieconsent_optout_marketing_build),
  ];
  $page['#attached']['library'][] = 'cookiebot/cookiebot';
}