cookiebot.module in Cookiebot - Cookie consent, Cookie monitoring and Cookie control 7
Same filename and directory in other branches
The Cookiebot main module file.
File
cookiebot.moduleView source
<?php
/**
* @file
* The Cookiebot main module file.
*/
/**
* Implements hook_help().
*/
function cookiebot_help($path, $arg) {
if ($path !== 'admin/help#cookiebot') {
return '';
}
$output = file_get_contents(drupal_get_path('module', 'cookiebot') . '/README.md');
return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
}
/**
* Implements hook_menu().
*/
function cookiebot_menu() {
$items = array();
$items['admin/config/system/cookiebot'] = array(
'title' => 'Cookiebot configuration',
'description' => 'Configure your Cookiebot integration, like the Domain Group ID.',
'access callback' => 'user_access',
'access arguments' => array(
'administer cookiebot',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'cookiebot_settings_form',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'cookiebot.admin.inc',
);
// Add a special menu item for the JavaScript powered Cookiebot renew link.
// This is not a page that can be visited, but this item makes it possible
// to place this special link in Drupal's menus.
$items['<cookiebot-renew>'] = array(
'page callback' => 'drupal_not_found',
// Since this is a JavaScript only link everybody has access.
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_permission().
*/
function cookiebot_permission() {
return array(
'administer cookiebot' => array(
'title' => t('Administer Cookiebot Settings'),
'description' => t('Allows users to administer Cookiebot settings.'),
),
'skip cookiebot consent' => array(
'title' => t('Can use the site without giving Cookiebot cookie consent'),
'description' => t('Allows users to use the site without giving Cookiebot cookie consent.'),
),
);
}
/**
* Implements hook_theme().
*/
function cookiebot_theme($existing, $type, $theme, $path) {
return [
'cookiebot_declaration' => [
'template' => 'templates/cookiebot-declaration',
'variables' => [
'cookiebot_src' => '',
],
],
'cookiebot_blocked_element_placeholder' => [
'template' => 'templates/cookiebot-blocked-element-placeholder',
'variables' => [
'content' => NULL,
],
],
];
}
/**
* Implements hook_page_alter().
*/
function cookiebot_page_alter(&$page) {
if (!_cookiebot_visible()) {
return;
}
$cookiebot_message_placeholder_cookieconsent_optout_marketing = variable_get('cookiebot_message_placeholder_cookieconsent_optout_marketing', [
'value' => '',
'format' => filter_default_format(),
]);
$message_placeholder_cookieconsent_optout_marketing_build = [
'#theme' => 'cookiebot_blocked_element_placeholder',
'#content' => check_markup($cookiebot_message_placeholder_cookieconsent_optout_marketing['value'], $cookiebot_message_placeholder_cookieconsent_optout_marketing['format']),
];
drupal_add_js([
'cookiebot' => [
'message_placeholder_cookieconsent_optout_marketing_show' => variable_get('cookiebot_message_placeholder_cookieconsent_optout_marketing_show') ? TRUE : FALSE,
'message_placeholder_cookieconsent_optout_marketing' => drupal_render($message_placeholder_cookieconsent_optout_marketing_build),
],
], 'setting');
drupal_add_js(drupal_get_path('module', 'cookiebot') . '/js/cookiebot.js');
drupal_add_css(drupal_get_path('module', 'cookiebot') . '/css/cookiebot.css', [
'every_page' => TRUE,
]);
}
function cookiebot_preprocess_html(&$variables) {
if (!_cookiebot_visible()) {
return;
}
$cbid = variable_get('cookiebot_cbid', '');
$attributes = [
'id' => 'Cookiebot',
'src' => 'https://consent.cookiebot.com/uc.js',
'data-cbid' => check_plain($cbid),
];
if (variable_get('cookiebot_block_cookies')) {
$attributes['data-blockingmode'] = 'auto';
}
else {
$attributes['async'] = 'async';
}
if (variable_get('cookiebot_iab_enabled')) {
$attributes['data-framework'] = 'IAB';
}
$cookiebot = array(
'#type' => 'markup',
'#markup' => '<script type="text/javascript" ' . drupal_attributes($attributes) . '></script>' . "\n",
'#weight' => -1001,
);
drupal_add_html_head($cookiebot, 'cookiebot_banner');
}
/**
* Helper function which returns true if cookiebot applies to the current page / session or false if true.
* Checks for visibulity settings, permissions and other relevant criteria.
*
* @return void
*/
function _cookiebot_visible() {
global $user;
$cbid = variable_get('cookiebot_cbid', '');
if (empty($cbid)) {
return FALSE;
}
// Check hide if user can use site without giving Cookiebot cookie consent.
if ($user->uid != 1 && user_access('skip cookiebot consent')) {
return FALSE;
}
// Check hide cookie for the superuser.
if ($user->uid == 1 && variable_get('cookiebot_exclude_uid_1', FALSE)) {
return FALSE;
}
// Check exclude paths.
$cookiebot_exclude_paths = variable_get('cookiebot_exclude_paths', '');
if (!empty($cookiebot_exclude_paths)) {
$path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
// Compare the lowercase internal and lowercase path alias (if any).
$page_match = drupal_match_path($path, $cookiebot_exclude_paths);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $cookiebot_exclude_paths);
}
if ($page_match) {
return FALSE;
}
}
// Check hide cookie compliance on admin theme.
if (variable_get('cookiebot_exclude_admin_theme', FALSE) && path_is_admin(current_path())) {
return FALSE;
}
return TRUE;
}
/**
* Implements hook_node_view_alter().
*/
function cookiebot_node_view_alter(&$build) {
$nid = $build['#node']->nid;
$declaration_node_id = variable_get('cookiebot_show_declaration_node_id', '');
$cbid = variable_get('cookiebot_cbid', '');
if ($nid != $declaration_node_id || $build['#view_mode'] !== 'full' || empty($cbid) || !variable_get('cookiebot_show_declaration', FALSE)) {
return;
}
// Print full cookies declaration from Cookiebot.
$build['cookiebot'] = array(
'#theme' => 'cookiebot_declaration',
'#cookiebot_src' => 'https://consent.cookiebot.com/' . $cbid . '/cd.js',
);
}
/**
* Implements hook_form_FROM_ID_alter() for menu_edit_item().
*/
function cookiebot_form_menu_edit_item_alter(&$form, &$form_state) {
if (!isset($form['link_path']['#default_value'])) {
return;
}
$form['link_path']['#description'] .= ' ' . t("Enter '%cookiebot-renew' to generate a link to renew Cookiebot's cookie consent status.", array(
'%cookiebot-renew' => '<cookiebot-renew>',
));
}
/**
* Implements hook_preprocess_HOOK() for menu_link().
*/
function cookiebot_preprocess_menu_link(&$variables) {
if (empty($variables['element']['#href']) || $variables['element']['#href'] !== '<cookiebot-renew>') {
return;
}
$variables['element']['#href'] = '/';
$variables['element']['#localized_options']['attributes']['class'][] = 'cookiebot-renew';
}
/**
* Implements hook_block_info().
*/
function cookiebot_block_info() {
$blocks = array();
$blocks['cookiebot_declaration'] = array(
'info' => t('Cookiebot declaration'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function cookiebot_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'cookiebot_declaration':
$cbid = variable_get('cookiebot_cbid', '');
if (empty($cbid)) {
break;
}
$block['content'] = array(
'#theme' => 'cookiebot_declaration',
'#cookiebot_src' => 'https://consent.cookiebot.com/' . $cbid . '/cd.js',
);
break;
}
return $block;
}
/**
* Implements hook_variable_info().
*/
function cookiebot_variable_info($options) {
$variables['cookiebot_cbid'] = array(
'type' => 'string',
'title' => t('Your cookiebot Domain Group ID (CBID)', array(), $options),
'localize' => FALSE,
);
$variables['cookiebot_block_cookies'] = [
'type' => 'boolean',
'title' => t('Automatically block all cookies', array(), $options),
];
$variables['cookiebot_iab_enabled'] = [
'type' => 'boolean',
'title' => t('Enabling IAB framework', array(), $options),
];
$variables['cookiebot_show_declaration'] = array(
'type' => 'boolean',
'title' => t('Show the Cookiebot cookie declaration', array(), $options),
);
$variables['cookiebot_show_declaration_node_id'] = array(
'type' => 'string',
'title' => t('Node ID', array(), $options),
'localize' => TRUE,
);
$variables['cookiebot_exclude_paths'] = [
'type' => 'text',
'title' => t('Exclude paths', array(), $options),
'localize' => TRUE,
];
$variables['cookiebot_exclude_admin_theme'] = [
'type' => 'boolean',
'title' => t('Exclude admin pages', array(), $options),
'localize' => FALSE,
];
$variables['cookiebot_exclude_uid_1'] = [
'type' => 'boolean',
'title' => t('Don’t show the Cookiebot for UID 1.', array(), $options),
'localize' => FALSE,
];
$variables['cookiebot_message_placeholder_cookieconsent_optout_marketing_show'] = [
'type' => 'boolean',
'title' => t('Show placeholder message for blocked marketing elements', array(), $options),
'localize' => FALSE,
];
$variables['cookiebot_message_placeholder_cookieconsent_optout_marketing'] = [
'type' => 'text',
'title' => t('Placebolder message for blocked marketing elements', array(), $options),
'element' => [
'#type' => 'text_format',
'#base_type' => 'textarea',
'#cardinality' => 1,
],
'localize' => TRUE,
];
return $variable;
}
Functions
Name | Description |
---|---|
cookiebot_block_info | Implements hook_block_info(). |
cookiebot_block_view | Implements hook_block_view(). |
cookiebot_form_menu_edit_item_alter | Implements hook_form_FROM_ID_alter() for menu_edit_item(). |
cookiebot_help | Implements hook_help(). |
cookiebot_menu | Implements hook_menu(). |
cookiebot_node_view_alter | Implements hook_node_view_alter(). |
cookiebot_page_alter | Implements hook_page_alter(). |
cookiebot_permission | Implements hook_permission(). |
cookiebot_preprocess_html | |
cookiebot_preprocess_menu_link | Implements hook_preprocess_HOOK() for menu_link(). |
cookiebot_theme | Implements hook_theme(). |
cookiebot_variable_info | Implements hook_variable_info(). |
_cookiebot_visible | Helper function which returns true if cookiebot applies to the current page / session or false if true. Checks for visibulity settings, permissions and other relevant criteria. |