Cookie consent application, like CookiePro or CookieBot, have automatic cookie blocking which makes it very simple to integrate a compliant cookie policy. The auto-blockers work by preventing scripts from running. This is not compatible with Drupal out-the-box as many scripts are required.
Usually these blockers allow tags to be added to the scripts to flag their purpose. This module provides a UI to flag any detected libraries from yml, etc, and additionally any script attachments added in other module's hook_page_attachments_alter().
Cookie application support is provided by plugins. This module comes with only one plugin - CookieBot, because that happens to be the service we use.
How to use this module
Once enabled, a menu entry in the Configuration / System menu will appear with a couple of configuration pages. One is to choose the cookie plugin, and the other is to categorise your site's scripts and libraries.
Aggregation
PLEASE NOTE - this WILL NOT work with JS / script aggregation. This is because Drupal will not aggregate scripts according to type. If anyone has time to tackle that issue here then please provide a patch and we can include that functionality.
Developer notes
Feel free to submit any other cookie provider plugins you think would be useful, e.g. CookiePro, etc. It's super easy and only needs a few lines of code per plugin. Take a look at the included CookieBot plugin for an example.
<?php
namespace Drupal\cookie_blocking_libraries\Plugin\CookieBlockingLibrariesImplementation;
use Drupal\cookie_blocking_libraries\CookieImplementationPluginBase;
/**
* Plugin implementation of the cookie_blocking_libraries_implementation.
*
* @CookieBlockingLibrariesImplementation(
* id = "cookie_bot",
* label = @Translation("Cookie Bot"),
* description = @Translation("Cookie Bot.")
* )
*/
class CookieBot extends CookieImplementationPluginBase {
/**
* {@inheritDoc}
*/
public function getCategories() : array {
return parent::getCategories() + [
'preferences' => $this->t('Preferences'),
'statistics' => $this->t('Statistics'),
'marketing' => $this->t('Marketing'),
'ignore' => $this->t('Ignore'),
];
}
/**
* {@inheritDoc}
*/
public function getDefaultCategory() : string {
return 'ignore';
}
/**
* {@inheritDoc}
*/
public function alterLibrary(array &$library_js, $extension, $type) : void {
$library_js['attributes']['data-cookieconsent'] = $type;
}
/**
* {@inheritDoc}
*/
public function alterAttachment(array &$html, $extension, $type) : void {
$html['#attributes']['data-cookieconsent'] = $type;
}
}
Any maintainers for cookie control modules can include the plugin in their modules if preferred.
The plugin only has to implement four methods
- getCategories() Return an array list of categorisations, e.g. Marketing, Statistics, etc.
- getDefaultCategory() Return a string of the default category, e.g. ignore
- alterLibrary() Change the library definition, e.g. add an attribute.
- alterAttachment() Change the attached script tag, e.g. add an attribute.
Project information
Seeking co-maintainer(s)
Maintainers are looking for help reviewing issues.- Project categories: Developer tools, Integrations, Site structure
- Ecosystem: Cookiebot - Cookie consent, Cookie monitoring and Cookie control, CookiePro by OneTrust , CookiePro Plus
- Created by dubs on , updated
Stable releases for this project are covered by the security advisory policy.
There are currently no supported stable releases.

