Developed with love by KnpLabs Hire us for your project!
33

cookie-consent-bundle

by ConnectHolland

Symfony bundle to append Cookie Consent to your website to comply to AVG/GDPR for cookies.

Scrutinizer Code Quality
Code Coverage
Build Status

Cookie Consent bundle for Symfony

Symfony bundle to append Cookie Consent to your website to comply to AVG/GDPR for cookies.

Installation

Step 1: Download using composer

In a Symfony application run this command to install and integrate Cookie Consent bundle in your application:
bash
composer require connectholland/cookie-consent-bundle

Step 2: Enable the bundle

When not using symfony flex, enable the bundle in the kernel manually:
```php
<?php
// app/AppKernel.php

public function registerBundles()
{
$bundles = array(
// ...
new ConnectHolland\CookieConsentBundle\CHCookieConsentBundle(),
// ...
);
}
```

Step 3: Enable the routing

When not using symfony flex, enable the bundles routing manually:
```yaml

app/config/routing.yml

ch_cookie_consent:
resource: "@CHCookieConsentBundle/Resources/config/routing.yaml"
```

Step 4: Configure to your needs

Configure your Cookie Consent with the following possible settings
yaml
ch_cookie_consent:
theme: 'light' # light, dark
categories: # Below are the default supported categories
- 'analytics'
- 'tracking'
- 'marketing'
- 'social_media'
use_logger: true # Logs user actions to database
position: 'top' # top, bottom

Usage

Twig implementation

Load the cookie consent in Twig via render_esi ( to prevent caching ) at any place you like:
twig
{{ render_esi(path('ch_cookie_consent.show')) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set')) }}

If you want to load the cookie consent with a specific locale you can pass the locale as a parameter:
twig
{{ render_esi(path('ch_cookie_consent.show', { 'locale' : 'en' })) }}
{{ render_esi(path('ch_cookie_consent.show_if_cookie_consent_not_set', { 'locale' : app.request.locale })) }}

Cookies

When a user submits the form the preferences are saved as cookies. The cookies have a lifetime of 1 year. The following cookies are saved:
- Cookie_Consent: date of submit
- Cookie_Consent_Key: Generated key as identifier to the submitted Cookie Consent of the user
- Cookie_Category_[CATEGORY]: selected value of user (*true* or false)

Logging

AVG/GDPR requires all given cookie preferences of users to be explainable by the webmasters. For this we log all cookie preferences to the database. IP addresses are anonymized. This option can be disabled in the config.

Database logging

Themes

Dark Theme
Light Theme

TwigExtension

The following TwigExtension functions are available:

chcookieconsent_isCategoryAllowedByUser
check if user has given it's permission for certain cookie categories
twig
{% if chcookieconsent_isCategoryAllowedByUser('analytics') == true %}
...
{% endif %}

chcookieconsent_isCookieConsentSavedByUser
check if user has saved any cookie preferences
twig
{% if chcookieconsent_isCookieConsentSavedByUser() == true %}
...
{% endif %}

Customization

Categories

You can add or remove any category by changing the config and making sure there are translations available for these categories.

Translations

All texts can be altered via Symfony translations by overwriting the CHCookieConsentBundle translation files.

Styling

CHCookieConsentBundle comes with a default styling. A sass file is available in Resources/assets/css/cookie_consent.scss and a build css file is available in Resources/public/css/cookie_consent.css. Colors can easily be adjusted by setting the variables available in the sass file.

To install these assets run:
bash
bin/console assets:install

And include the styling in your template:
twig
{% include "CHCookieConsentBundle::cookie_consent_styling.html.twig" %}

Javascript

By loading Resources/public/js/cookie_consent.js the cookie consent will be submitted via ajax and the cookie consent will be shown on top of your website while pushing down the rest of the website.

Template Themes

You can override the templates by placing templates inside your poject:

# app/Resources/CHCookieConsentBundle/views/cookie_consent.html.twig
{% extends '@!CHCookieConsent/cookie_consent.html.twig' %}

{% block title %}
    Your custom title
{% endblock %}
MIT License

Copyright (c) 2019 Connect Holland

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • Merge pull request #51 from matthijsch/support-twig-3
    By web-flow, 1 month ago
  • Fix for deprecated Twig function
    By matthijsch, 1 month ago
  • Merge pull request #50 from matthijsch/support-twig-3
    By web-flow, 1 month ago
  • Support Twig ^3.0
    By matthijsch, 1 month ago
  • Fix unfound ResponseEvent class bug (#49)
    By web-flow, 1 month ago
  • Add missing unit test
    By matthijsch, 1 month ago
  • Fix unfound ResponseEvent class bug
    By matthijsch, 1 month ago
  • bugfix dependency injection of wrong and deprecated classes (#45)
    By web-flow, 3 months ago
  • bugfix dependency injection of wrong and deprecated classes
    By , 3 months ago
  • Fix Symfony 5 & doctrine bundle 2 support (#44)
    By web-flow, 3 months ago
  • Fix ResponseEvent typehint for different symfony versions
    By , 3 months ago
  • Remove translation contract for symfony 3.4 support
    By matthijsch, 3 months ago
  • Change class of kernelResponse event
    By matthijsch, 3 months ago
  • Use symfony contracts for translator
    By matthijsch, 3 months ago
  • Fix doctrine bundle 2 support
    By matthijsch, 3 months ago
  • Support doctrine bundle ^2.0 (#42)
    By web-flow, 3 months ago
  • Support doctrine bundle 2.*
    By matthijsch, 3 months ago
  • Set minimum version of Symfony 4 to 4.3 & add Symfony 5 support (#40)
    By web-flow, 4 months ago
  • Lower symfony 4 requirements to 4.2 as support lasts till end of january
    By matthijsch, 4 months ago
  • Fix return type hints in tests
    By matthijsch, 4 months ago
  • Add symfony 5 support
    By matthijsch, 4 months ago
  • Update minimum version of symfony 4 to 4.3
    By matthijsch, 4 months ago
  • Fix event dispatcher bug and deprecations (#39)
    By web-flow, 4 months ago
  • Fix event dispatcher bug and deprecations
    By matthijsch, 4 months ago
  • Fix README for old twig function name (#37)
    By web-flow, 5 months ago
  • Fix README for old twig function name
    By matthijsch, 5 months ago
  • Merge pull request #35 from jaimybroere/add-position-option
    By web-flow, 5 months ago
  • Use if else and update README
    By , 5 months ago
  • Fix tests
    By jaimybroere, 5 months ago
  • Remove if statement
    By jaimybroere, 5 months ago