simple_cookie_compliance.module in Simple Cookie Compliance 7
Same filename and directory in other branches
All the necessary functions for configuring and output.
File
simple_cookie_compliance.moduleView source
<?php
/**
* @file
* All the necessary functions for configuring and output.
*/
/**
* Implements hook_menu().
*/
function simple_cookie_compliance_menu() {
$items = array();
$items['admin/config/system/simple-cookie-compliance'] = array(
'title' => 'Cookie Compliance Settings',
'description' => 'Settings for the cookie compliance window that is used.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'_simple_cookie_compliance_settings_form',
),
'access arguments' => array(
'administer cookie compliance',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function simple_cookie_compliance_permission() {
return array(
'administer cookie compliance' => array(
'title' => t('Administer Cookie Compliance'),
'description' => t('Perform administration tasks for Cookie Compliance.'),
),
);
}
/**
* Implements hook_theme().
*/
function simple_cookie_compliance_theme($existing, $type, $theme, $path) {
return array(
'cookie_compliance' => array(
'template' => 'templates/cookie-compliance',
),
);
}
function template_preprocess_cookie_compliance(&$vars) {
global $language;
// The text to show in the area.
$message = variable_get('simple_cookie_compliance_content_' . $language->language);
if (empty($message)) {
$message['value'] = t('This website uses cookies to help us give you the best experience when you visit our website. By continuing to use this website, you consent to our use of these cookies.');
}
$vars['message'] = filter_xss($message['value']);
// The form and submit button.
$button_text = check_plain(variable_get('simple_cookie_compliance_button_agree_' . $language->language, t('OK')));
$vars['form'] = drupal_get_form('simple_cookie_compliance_dismiss_form');
}
/**
* Implements hook_page_alter().
*/
function simple_cookie_compliance_page_alter(&$page) {
global $language;
// Add the text and form.
// This lives in a theme-function.
$page['page_top']['simple_cookie_compliance'] = array(
'#markup' => theme('cookie_compliance'),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'simple_cookie_compliance') . '/js/simple_cookie_compliance.js' => array(
'every_page' => TRUE,
),
),
),
);
}
/**
* Dismiss form.
*/
function simple_cookie_compliance_dismiss_form($form, $form_state) {
global $language;
$button_text = check_plain(variable_get('simple_cookie_compliance_button_agree_' . $language->language, t('OK')));
$form = array();
$form['submit'] = array(
'#type' => 'submit',
'#value' => $button_text,
'#attributes' => array(
'class' => array(
'cookie-compliance__button',
),
),
'#ajax' => array(
'callback' => 'simple_cookie_compliance_dismiss_form_submit',
'progress' => array(
'type' => 'none',
),
'wrapper' => 'cookie-compliance',
),
);
$form['#attached']['css'] = array(
drupal_get_path('module', 'simple_cookie_compliance') . '/css/simple_cookie_compliance.css',
);
return $form;
}
/**
* Default cookie expiration time
*/
function _simple_cookie_compliance_default_expiration_time() {
$three_months = 60 * 60 * 24 * 30 * 3;
return $three_months;
}
/**
* Submit handler for dismiss form.
*/
function simple_cookie_compliance_dismiss_form_submit($form, $form_state) {
// Set the cookie with the expiration time defined in module settings.
$default_expiration_time = _simple_cookie_compliance_default_expiration_time();
$expiration_time = REQUEST_TIME + variable_get('simple_cookie_compliance_expiration_time', $default_expiration_time);
setcookie('simple_cookie_compliance_dismissed', 1, $expiration_time, '/');
// The ajax call wants something to replace HTML-element with. Since we in
// this case want to remove it, we replace it with nothing.
return '';
}
/**
* Custom form function.
*/
function _simple_cookie_compliance_settings_form() {
$languages = language_list();
// Cookie expiration.
$form = array();
$default_expiration_time = _simple_cookie_compliance_default_expiration_time();
$form['simple_cookie_compliance_expiration_time'] = array(
'#type' => 'textfield',
'#title' => t('Expiration time'),
'#description' => t('The time it will take to expire the cookie in seconds. Default value is equal to three months.'),
'#default_value' => variable_get('simple_cookie_compliance_expiration_time', $default_expiration_time),
'#required' => TRUE,
);
foreach ($languages as $language) {
if ($language->enabled) {
// Field group per language.
$form['language_' . $language->language] = array(
'#type' => 'fieldset',
'#title' => check_plain($language->name),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// Text setting for message.
$message = variable_get('simple_cookie_compliance_content_' . $language->language);
if (empty($message)) {
$message['value'] = t('This website uses cookies to help us give you the best experience when you visit our website. By continuing to use this website, you consent to our use of these cookies.');
}
$form['language_' . $language->language]['simple_cookie_compliance_content_' . $language->language] = array(
'#type' => 'text_format',
'#format' => 'full_html',
'#title' => t('Message'),
'#default_value' => $message['value'],
'#required' => TRUE,
);
// Text setting for agree button.
$form['language_' . $language->language]['simple_cookie_compliance_button_agree_' . $language->language] = array(
'#type' => 'textfield',
'#title' => t('Agree button'),
'#default_value' => variable_get('simple_cookie_compliance_button_agree_' . $language->language, t('OK')),
'#required' => TRUE,
);
}
}
return system_settings_form($form);
}
Functions
Name | Description |
---|---|
simple_cookie_compliance_dismiss_form | Dismiss form. |
simple_cookie_compliance_dismiss_form_submit | Submit handler for dismiss form. |
simple_cookie_compliance_menu | Implements hook_menu(). |
simple_cookie_compliance_page_alter | Implements hook_page_alter(). |
simple_cookie_compliance_permission | Implements hook_permission(). |
simple_cookie_compliance_theme | Implements hook_theme(). |
template_preprocess_cookie_compliance | |
_simple_cookie_compliance_default_expiration_time | Default cookie expiration time |
_simple_cookie_compliance_settings_form | Custom form function. |