You are here

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

Same name and namespace in other branches
  1. 7 cookiebot.module \cookiebot_node_view_alter()

Implements hook_node_view_alter().

File

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

Code

function cookiebot_node_view_alter(array &$build) {
  $config = \Drupal::config('cookiebot.settings');
  $cbid = $config
    ->get('cookiebot_cbid');
  $build['#cache']['tags'] = Cache::mergeTags(!empty($build['#cache']['tags']) ? $build['#cache']['tags'] : [], [
    'cookiebot:cbid',
    'cookiebot:show_declaration',
  ]);
  if (!$config
    ->get('cookiebot_show_declaration') || empty($cbid)) {
    return;
  }
  $declaration_node_path = $config
    ->get('cookiebot_show_declaration_node_path');
  $current_nid = \Drupal::routeMatch()
    ->getRawParameter('node');
  $current_alias = \Drupal::service('path_alias.manager')
    ->getAliasByPath('/node/' . $current_nid);

  // Print full cookies declaration from Cookiebot.
  if ($current_alias === $declaration_node_path) {
    $build['cookiebot'][] = [
      '#theme' => 'cookiebot_declaration',
      '#cookiebot_src' => 'https://consent.cookiebot.com/' . $cbid . '/cd.js',
    ];
  }
}