function cookieconsent_page_attachments in CookieConsent 8
Implements hook_page_attachments().
File
- ./
cookieconsent.module, line 47 - Contains cookieconsent.module.
Code
function cookieconsent_page_attachments(array &$attachments) {
// Does the popup has to be shown for this user?
$current_user = \Drupal::currentUser();
$exclude_from_consent = $current_user
->hasPermission('exclude from cookieconsent');
// If not then proceed.
if (!$exclude_from_consent) {
$config = \Drupal::config('cookieconsent.settings');
// Add the CookieConsent options.
$attachments['#attached']['library'][] = 'cookieconsent/settings';
if ($config
->get('customise')) {
// Set our custom texts.
$attachments['#attached']['drupalSettings']['cookieconsent']['message'] = $config
->get('headline_text');
$attachments['#attached']['drupalSettings']['cookieconsent']['dismiss'] = $config
->get('accept_button_text');
$attachments['#attached']['drupalSettings']['cookieconsent']['learnMore'] = $config
->get('read_more_button_text');
}
$alias = '';
$node_id = $config
->get('cookie_policy');
if (is_numeric($node_id)) {
$alias = Url::fromRoute('entity.node.canonical', [
'node' => $node_id,
])
->toString();
}
$attachments['#attached']['drupalSettings']['cookieconsent']['link'] = $alias;
$attachments['#attached']['drupalSettings']['cookieconsent']['path'] = $config
->get('path');
$attachments['#attached']['drupalSettings']['cookieconsent']['expiry'] = (int) $config
->get('expiry');
$attachments['#attached']['drupalSettings']['cookieconsent']['target'] = $config
->get('target');
// Set the cookie domain.
/** @var \Drupal\Core\Session\SessionConfiguration $session_confguration */
$session_configuration = \Drupal::service('session_configuration');
$session_configuration_options = $session_configuration
->getOptions(\Drupal::request());
$attachments['#attached']['drupalSettings']['cookieconsent']['domain'] = $session_configuration_options['cookie_domain'];
// Render the template and pass it over to our javascript.
/** @var Drupal\Core\Render\Renderer $renderer */
$renderer = \Drupal::service('renderer');
$element = [
'#theme' => 'cookieconsent',
'#dismiss' => $config
->get('accept_button_text'),
'#message' => $config
->get('headline_text'),
'#target' => $config
->get('target'),
'#link' => $alias,
'#learn_more' => $config
->get('read_more_button_text'),
];
/** @var Drupal\Core\Render\Markup $markup */
$markup = $renderer
->renderRoot($element);
// Cast to string (Uses the magic __toString method).
$attachments['#attached']['drupalSettings']['cookieconsent']['markup'] = (string) $markup;
// Determine if a container is used.
$container = $config
->get('container');
if (!empty($container)) {
$attachments['#attached']['drupalSettings']['cookieconsent']['container'] = $config
->get('container');
}
else {
$attachments['#attached']['drupalSettings']['cookieconsent']['container'] = NULL;
}
// Determine what theme to use.
$theme = $config
->get('theme');
if ($theme === 'none') {
$attachments['#attached']['drupalSettings']['cookieconsent']['theme'] = $config
->get('theme_path');
}
else {
// Add our CSS library that contains the CSS of the chosen theme
// (see cookieconsent.libraries.yml).
$attachments['#attached']['library'][] = 'cookieconsent/' . $theme;
// Set it to FALSE, so no external theme is being loaded.
$attachments['#attached']['drupalSettings']['cookieconsent']['theme'] = FALSE;
}
// Add the CookieConsent library itself.
if ($config
->get('minified')) {
$attachments['#attached']['library'][] = 'cookieconsent/cookieconsent-min';
}
else {
$attachments['#attached']['library'][] = 'cookieconsent/cookieconsent';
}
}
}