An open API service indexing awesome lists of open source software.

https://github.com/statikbe/craft-cookie-banner


https://github.com/statikbe/craft-cookie-banner

craft-plugin craftcms

Last synced: about 1 year ago
JSON representation

Awesome Lists containing this project

README

          

# Cookie banner for Craft CMS 3.x

This is an internal Statik.be plugin for use with our Tag Manager consent management solution.

⚠️ The plugin will only render templates out of the box, it won't do anything to prevent tracking without proper configuration.

## Requirements

This plugin requires Craft CMS 3.0.0 or later.

## Installation

To install the plugin, follow these instructions:

```console
cd /path/to/project
composer require statikbe/craft-cookie-banner
./craft plugin/install cookie-banner
```

## Usage

The plugin uses a Twig function to render the initial popup and the cookie settings modal.

```PHP
{{ craft.cookieBanner.render() }}
```

You can also pass an array of options:

- modal: path to the template for the modal
- banner: path to the template for the banner
- overlay: HTML string with styling that will you use as overlay behind the banner
- supportIE: wether or not the Javascript used should be compiled for ES6 or ES5 (defaults to false so using ES6)
- cookiePage: the entry where the cookie policy content is defined. This is used on the modal template to show the possible settings.

For example:

```PHP
{{ craft.cookieBanner.render({
modal: '_site/_snippet/_cookie/_modal',
banner: '_site/_snippet/_cookie/_banner',
overlay: '

',
supportIE: true,
cookiePage: craft.entries.section('cookiePolicy').one(),
showCookieBanner: (entry.type is defined and entry.type.handle != 'cookiePolicy')
}) }}
```

It's adviced to not overwrite the templates just for styling, only for more fundamental and structural changes.

[Modal](https://github.com/statikbe/craft-cookie-banner/blob/master/src/templates/_modal.twig), [banner](https://github.com/statikbe/craft-cookie-banner/blob/master/src/templates/_banner.twig) and [overlay](https://github.com/statikbe/craft-cookie-banner/blob/develop/src/templates/_overlay.twig) templates have generic classes that can be used for styling.

To trigger the settings modal after closing the cookie banner, you can use the following code:

```HTML
{{ 'Cookie settings'|t }}
```

## Listeners

You can listen to the following events:

```
cookie-banner-opened
cookie-closed
cookie-modal-opened
cookie-prop-performance-disabled
cookie-prop-performance-enabled
cookie-prop-marketing-disabled
cookie-prop-marketing-enabled
```

Example:

```js
window.addEventListener('cookie-closed', () => {
console.log('cookie-closed');
});
```