cookies.module in COOKiES Consent Management 1.0.x
Contains cookies.module.
File
cookies.moduleView source
<?php
/**
* @file
* Contains cookies.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\UserInterface;
/**
* Implements hook_help().
*/
function cookies_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.cookies':
$text = file_get_contents(__DIR__ . '/README.md');
if (!\Drupal::moduleHandler()
->moduleExists('markdown')) {
return '<pre>' . Html::escape($text) . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()
->get('markdown.settings')
->getRawData();
$config = [
'settings' => $settings,
];
$filter = $filter_manager
->createInstance('markdown', $config);
return $filter
->process($text, 'en');
}
}
return NULL;
}
/**
* Implements hook_theme().
*/
function cookies_theme() {
return [
'cookies_container' => [
'variables' => [
'id' => NULL,
'styles' => NULL,
],
],
'cookies_docs_group' => [
'variables' => [
'attributes' => [],
'label' => NULL,
'items' => [],
],
],
'cookies_docs_service' => [
'variables' => [
'attributes' => [],
'label' => NULL,
'external_link_url' => [],
'external_link_text' => [],
'content' => [],
'edit_link_url' => [],
'edit_link_text' => [],
],
],
];
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function cookies_menu_links_discovered_alter(&$links) {
/*
* Define the "Cookie settings" menu item in Tools.
* Dynamic equivalent of links.menu.yml entry, but we need to set the URL
* dynamically because the dialog open fragment can be configured in COOKiES
* settings.
*
* cookies.open_cookie_consent_dialog:
* title: "Cookie settings"
* description: "Open the cookie consent dialog."
* url: "internal:<DYNAMIC:#editCookieSettings>"
* menu_name: tools
* options:
* attributes:
* class:
* - cookies-open-cookie-consent-dialog
*/
$open_settings_hash = \Drupal::config('cookies.config')
->get('open_settings_hash');
if (!empty($open_settings_hash)) {
$links['cookies.open_cookie_consent_dialog'] = [
'title' => new TranslatableMarkup("Cookie settings"),
'description' => new TranslatableMarkup("Open the cookie consent dialog."),
'url' => "internal:#" . htmlspecialchars($open_settings_hash, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'),
'menu_name' => 'tools',
'provider' => 'cookies',
'options' => [
'attributes' => [
'class' => 'cookies-open-cookie-consent-dialog',
],
],
];
}
}
/**
* Implements hook_cookies_user_consent().
*
* Changes of COOKiES cookie content are stored in the db if user is
* authenticated. So his settings can be revived in sessions on other browsers.
*/
function cookies_cookies_user_consent($consent) {
$account = \Drupal::currentUser();
if ($account
->isAuthenticated() && \Drupal::config('cookies.config')
->get('store_auth_user_consent')) {
$key = "uid:{$account->id()}";
$store = \Drupal::keyValue('cookies.consent.user');
$store
->set($key, $consent);
return [
'cookies' => [
'status' => t('Your cookie settings have been saved in your account.'),
],
];
}
return [];
}
/**
* Implements hook_user_login().
*
* If COOKiES cookie exists save/update content for user in key-value storage.
* If not, check key-value storage, if cookie content is stored and re-install
* cookie if exists.
*/
function cookies_user_login(UserInterface $account) {
$config = \Drupal::config('cookies.config');
if ($config
->get('store_auth_user_consent')) {
/** @var string $cookie_name */
$cookie_name = $config
->get('cookie_name');
$store = \Drupal::keyValue('cookies.consent.user');
$key = "uid:{$account->id()}";
if ($cookie_content = \Drupal::request()->cookies
->get($cookie_name)) {
// User has set or update cookie before logged in.
// => Update cookie consent in db.
$store
->set($key, json_decode($cookie_content, true));
}
elseif ($cookie_content = $store
->get($key)) {
// User gets COOKiES-cookie from storage.
$expires = $config
->get('cookie_expires') ?? 365;
$options = [
"expires" => \Drupal::time()
->getRequestTime() + $expires * 24 * 60 * 60,
"path" => "/",
"domain" => $config
->get('cookie_domain') ?? "",
"secure" => $config
->get('cookie_secure') ?? false,
"samesite" => $config
->get('cookie_same_site') ?? "Lax",
];
setcookie($cookie_name, json_encode($cookie_content), $options);
}
}
}
Functions
Name | Description |
---|---|
cookies_cookies_user_consent | Implements hook_cookies_user_consent(). |
cookies_help | Implements hook_help(). |
cookies_menu_links_discovered_alter | Implements hook_menu_links_discovered_alter(). |
cookies_theme | Implements hook_theme(). |
cookies_user_login | Implements hook_user_login(). |