You are here

simple_cookie_compliance.module in Simple Cookie Compliance 8

Same filename and directory in other branches
  1. 6 simple_cookie_compliance.module
  2. 7 simple_cookie_compliance.module

File

simple_cookie_compliance.module
View source
<?php

/**
 * @file
 * Contains simple_cookie_compliance.module.
 */

/**
 * Implements hook_page_attachments().
 */
function simple_cookie_compliance_page_attachments(array &$attachments) {
  if (\Drupal::currentUser()
    ->isAnonymous()) {
    $attachments['#attached']['library'][] = 'simple_cookie_compliance/simple_cookie_compliance';
  }
}

/**
 * Implements hook_theme().
 */
function simple_cookie_compliance_theme() {
  return [
    'simple_cookie_compliance' => [
      'variables' => [
        'agree_button' => NULL,
        'message_text' => NULL,
        'use_no_script' => TRUE,
      ],
    ],
  ];
}

/**
 * Implements hook_page_top().
 */
function simple_cookie_compliance_page_top(array &$page_top) {
  $config = \Drupal::config('simple_cookie_compliance.settings');
  $use_no_script = $config
    ->get('no_script', TRUE);
  $message = $config
    ->get('message.value');
  $message_format = $config
    ->get('message.format');
  $agree_button = $config
    ->get('agree_button');
  if ($use_no_script) {
    $page_top['simple_cookie_compliance'] = [
      '#theme' => 'simple_cookie_compliance',
      '#use_no_script' => $use_no_script,
      '#message_text' => check_markup($message, $message_format),
    ];
  }
  $js_template = [
    '#theme' => 'simple_cookie_compliance',
    '#use_no_script' => FALSE,
    '#message_text' => check_markup($message, $message_format),
    '#agree_button' => $agree_button,
  ];
  $rendered = \Drupal::service('renderer')
    ->renderPlain($js_template);
  $expires = $config
    ->get('expires');
  $page_top['#attached']['drupalSettings']['simple_cookie_compliance']['template'] = $rendered;
  $page_top['#attached']['drupalSettings']['simple_cookie_compliance']['expires'] = $expires;
}