4 * This module intends to deal with the EU Directive on Privacy and Electronic
5 * Communications that comes into effect in the UK on 26th May 2012.
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Path\AliasManager;
10 use Drupal\Core\Path\AliasStorage;
11 use Drupal\Core\Path\AliasWhitelist;
12 use Drupal\Core\Extension\ModuleHandler;
13 use Drupal\Core\Database\Database;
14 use Drupal\Core\Cache\Cache;
15 use Drupal\Core\Cache\DatabaseBackend;
16 use Drupal\Core\Language\LanguageManager;
18 use Drupal\Component\Utility\SafeMarkup;
19 use Drupal\Component\Utility\Unicode;
20 use Drupal\Component\Utility\Html;
23 * Implements hook_page_attachments().
25 function eu_cookie_compliance_page_attachments(&$attachments) {
26 $config = \Drupal::config('eu_cookie_compliance.settings');
28 // Check Add/Remove domains.
30 $domain_option = $config->get('domains_option');
32 if (!empty($config->get('domains_list'))) {
35 $domains_list = str_replace(array("\r\n", "\r"), "\n", $config->get('domains_list'));
36 $domains_list = explode("\n", $domains_list);
37 $domain_match = in_array($base_url, $domains_list);
39 if ($domain_option && $domain_match) {
40 $domain_allow = FALSE;
43 // @TODO: Ask about this thing.
44 if (!$domain_option && !$domain_match) {
45 $domain_allow = FALSE;
49 // Check exclude paths.
52 if (!empty($config->get('exclude_paths'))) {
53 // @TODO: make this more robust.
54 $path = \Drupal::service('path.current')->getPath();
55 $path_match = \Drupal::service('path.matcher')->matchPath($path, $config->get('exclude_paths'));
58 // Unsupported GeoIP feature until those modules update their code.
61 if ($config->get('popup_enabled') && \Drupal::currentUser()->hasPermission('display eu cookie compliance popup') && $geoip_match && $domain_allow && !$path_match) {
62 $language = Drupal::languageManager()->getCurrentLanguage();
63 // Array storage for caching full client data.
66 if ($cache = \Drupal::cache()->get('eu_cookie_compliance_client_settings.' . $language->getId())) {
70 // Initialize some needed popup settings messages.
71 $popup_settings_messages = array(
72 'popup_agree_button_message',
73 'popup_disagree_button_message',
74 'popup_hide_button_message',
75 'popup_find_more_button_message'
77 foreach ($popup_settings_messages as $key) {
78 if (!isset($popup_settings[$key])) {
79 $popup_settings[$key] = '';
84 if ($config->get('popup_bg_hex') !== '' and $config->get('popup_text_hex') !== '') {
85 $position = $config->get('popup_position') ? 'top' : 'bottom';
86 $data['css'] = '#sliding-popup.sliding-popup-' . $position . ' {background:#' . Html::escape($config->get('popup_bg_hex')) . ';}'
87 . '#sliding-popup .popup-content #popup-text h2, #sliding-popup .popup-content #popup-text p {color:#' . Html::escape($config->get('popup_text_hex')) . ' !important;}';
89 $popup_text_info = str_replace(array("\r", "\n"), '', $config->get('popup_info.value'));
90 $popup_text_agreed = str_replace(array("\r", "\n"), '', $config->get('popup_agreed.value'));
92 '#theme' => 'eu_cookie_compliance_popup_info',
93 '#message' => check_markup($popup_text_info, $config->get('popup_info.format'), FALSE),
94 '#agree_button' => $config->get('popup_agree_button_message'),
95 '#disagree_button' => $config->get('popup_disagree_button_message'),
98 '#theme' => 'eu_cookie_compliance_popup_agreed',
99 '#message' => check_markup($popup_text_agreed, $config->get('popup_agreed.format'), FALSE),
100 '#hide_button' => $config->get('popup_hide_button_message'),
101 '#find_more_button' => $config->get('popup_find_more_button_message'),
104 $was_debugging = FALSE;
107 * @var $twig_service Twig_Environment
109 $twig_service = \Drupal::service('twig');
111 if ($twig_service->isDebug()) {
112 $was_debugging = TRUE;
113 $twig_service->disableDebug();
116 $html_info = \Drupal::service('renderer')->renderRoot($html_info)->__toString();
117 $html_agreed = \Drupal::service('renderer')->renderRoot($html_agreed)->__toString();
119 if ($was_debugging) {
120 $twig_service->enableDebug();
123 $data['variables'] = array(
124 'popup_enabled' => $config->get('popup_enabled'),
125 'popup_agreed_enabled' => $config->get('popup_agreed_enabled'),
126 'popup_hide_agreed' => $config->get('popup_hide_agreed'),
127 'popup_clicking_confirmation' => $config->get('popup_clicking_confirmation'),
128 'popup_html_info' => $config->get('popup_enabled') ? $html_info : FALSE,
129 'popup_html_agreed' => $config->get('popup_agreed_enabled') ? $html_agreed : FALSE,
130 'popup_height' => !empty($config->get('popup_height')) ? $config->get('popup_height') : 'auto',
131 'popup_width' => !empty($config->get('popup_width')) ? $config->get('popup_width') : '100%',
132 'popup_delay' => (int) ($config->get('popup_delay') * 1000),
133 'popup_link' => $config->get('popup_link'),
134 'popup_link_new_window' => $config->get('popup_link_new_window'),
135 'popup_link' => $config->get('popup_link'),
136 'popup_link_new_window' => !empty($config->get('popup_link_new_window')) ? $config->get('popup_link_new_window') : 1,
137 'popup_position' => $config->get('popup_position'),
138 'popup_language' => $language->getId(),
139 'popup_bg_hex' => !empty($config->get('popup_bg_hex')) ? $config->get('popup_bg_hex') : FALSE,
140 'popup_text_hex' => !empty($config->get('popup_text_hex')) ? $config->get('popup_text_hex') : FALSE,
141 'domain' => $config->get('domain'),
143 \Drupal::cache()->set('eu_cookie_compliance_client_settings.' . $language->getId(), $data);
146 $attachments['#attached']['drupalSettings']['eu_cookie_compliance'] = $data['variables'];
147 $attachments['#attached']['library'][] = 'eu_cookie_compliance/eu_cookie_compliance';
148 $cache_tags = isset($attachments['#cache']['tags']) ? $attachments['#cache']['tags'] : [];
149 $attachments['#cache']['tags'] = Cache::mergeTags($cache_tags, $config->getCacheTags());
154 * Implements hook_theme().
156 function eu_cookie_compliance_theme($existing, $type, $theme, $path) {
158 'eu_cookie_compliance_popup_info' => array(
159 'template' => 'eu_cookie_compliance_popup_info',
160 'variables' => array(
162 'agree_button' => NULL,
163 'disagree_button' => NULL
166 'eu_cookie_compliance_popup_agreed' => array(
167 'template' => 'eu_cookie_compliance_popup_agreed',
168 'variables' => array(
170 'hide_button' => NULL,
171 'find_more_button' => NULL
178 * Validate field for a HEX value if a value is set.
180 function eu_cookie_compliance_validate_hex($element, FormStateInterface &$form_state) {
181 if (!empty($element['#value']) && !preg_match('/^[0-9a-fA-F]{3,6}$/', $element['#value'])) {
182 $form_state->setError($element, t('%name must be a HEX value (without leading #) or empty.', array('%name' => $element['#title'])));