A PHP MODX Revolution extra to automatically comply with EU cookie law

Last modified: 08/03/2016 - 1 year ago

Requirements: MODX Revolution 2.2+ | PHP5+

Download: MODX Revolution Installer | MODX Extras Repository

A project tagged as: modx, plugin

Twitter Google+ Facebook Reddit

Overview

For sites that have visitors in the European Union (EU), the EU cookie law states that each visitor must give their consent for most cookies to be used. The usefulness or benefit of this is a devisive subject!

CookieConsent displays a message informing each visitor that the site uses cookies, with a link to your site's cookie policy.

Plugins

Property Description Default
cookieDays The number of days the cookie should last for 365
cookieName The name of the cookie that determines whether consent has been given CookieConsent
idCookiePolicy ID of resource containing site cookie policy
tpl The name of the chunk containing the cookie disclaimer cookieConsent
class The name of the class selector for the CookieConsent HTML cookieconsent
includeCSS Whether to include the CSS file
includeJS Whether to include the JavaScript file
pathCSS The location of the CSS file. Use the {assets_path} placeholder as a shortcut. Must include ".css" {assets_path}/.../cookieconsent-min.css
pathJS The location of the JavaScript file. Use the {assets_path} placeholder as a shortcut. Must include ".js" {assets_path}/.../mabCookieSet-min.js

The CookieConsent plugin is run on the OnWebPagePrerender system event.

Custom properties

Setting your own custom properties for a MODX plugin takes a bit more effort than that required for a snippet.

To set your own properties, do the following:-

  • Open the CookieConsent plugin and go to the Properties tab
  • Add a new Property Set
  • Set your custom values
  • Now go to the System Events tab
  • Update the Property Set field for the OnWebPagePrerender with the name of your custom Propert Set

You will need to update the OnWebPagePrerender event with the name of your custom Property Set any time you upgrade your CookieConsent plugin. This is the way MODX handles the upgrade of a package plugin. However, the values you set in your custom Property Set will be saved between upgrades.

Example

By default, CookieConsent appends a disclaimer to the body tag if consent has not been set. The disclaimer is positioned using CSS.

Chunks

CookieConsent uses a simple chunk which contains the cookie disclaimer message and the call to the jQuery plugin that will save their response to a cookie.

CookieConsent

The chunk displayed to the user. Can be set using the tpl property. It uses a Bootstrap theme.

<div data-name="[[+cookieName]]" data-value="[[+cookieValue]]" data-days="[[+cookieDays]]"  class="alert alert-warning alert-dismissible fade in [[+class]]" role="alert">
  
  <p class="text-center">
    We have placed cookies on your device to help make this website better. Your can view our <a title="View out cookie policy" href="[[+idCookiePolicy:gt=`0`:then=`[[~[[+idCookiePolicy]]]]`:else=`/`]]">cookie policy here</a>.     
    
    <button type="button" class="btn btn-info" data-dismiss="alert">Continue using site</button>
  </p>
  
</div>

<script>
  $(function() 
  {
    $('.[[+class]]').on('closed.bs.alert', function ()
    {
      $(this).mabCookieSet();
    });
  });
</script>

To customise CookieConent, simply make your own chunk based on the one above, and name it CookieConsent.

jQuery plugin

CookieConsent includes a jQuery plugin that sets a cookie so that it remembers whether the current visitor has accepted your cookie policy.

Usage

The jQuery plugin is automatically included unless includeJS is set to false. You might do this if you have your own script you'd like to use to add a cookie. If so, you could instead set pathJS which will automatically include your own script.

The mabCookieSet plugin can be triggered via JavaScript. It is triggered for you in the defualt chunk, as detailed above.

$( ".cookieconsent" ).mabCookieSet();

Options

Options are passed via data attributes or set directly in the call as options. For data attributes, append the option name to data-, as in data-name="CookieConsent".

Name Type Default Description
name string CookieConsent The name of the cookie
value string   The value of the cookie
days number 365 The number of days the cookie will last for

References

Licence

CookieConsent by mad about brighton is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Permissions beyond the scope of this license may be available by contacting me.