Git Product home page Git Product logo

yii2-gdpr-cookie-consent's Introduction

GDPR compatible Cookie Consent widget for Yii 2.0 Framework

GDPR compatible Cookie Consent widget allows the user to choose which kind of cookies they want to accept.

Build Status

Installation

The preferred way to install this extension is through composer.

Run

composer require --prefer-dist albertborsos/yii2-gdpr-cookie-consent

Usage

add the component to your config file:

<?php
return [
    // ...
    'components' => [
        // ...
        'cookieConsent' => [
            'class' => \albertborsos\cookieconsent\Component::class,
            'urlSettings' => ['/site/cookie-settings'],
            'urlPrivacyPolicy' => ['/site/privacy-policy'],
            'documents' => [
                ['name' => 'Privacy Policy', 'url' => ['/docs/privacy-policy.pdf']],
            ],
            'disabledCategories' => [
                \albertborsos\cookieconsent\helpers\CookieHelper::CATEGORY_BEHAVIOR,
            ],
        ],
        // ...
        'i18n' => [
            // ...
            'translations' => [
                'cookieconsent/*' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@vendor/albertborsos/yii2-gdpr-cookie-consent/src/messages',
                ],
            ],
            // ...
        ],
    ],
    // ...
];

Register the widget in your layout. For example in a _cookieconsent.php partial view.

<?php
/** @var \albertborsos\cookieconsent\Component $component */
$component = Yii::$app->cookieConsent;
$component->registerWidget([
    'policyLink' => ['/default/cookie-settings'],
    'policyLinkText' => \yii\helpers\Html::tag('i', null, ['class' => 'fa fa-cog']) . ' Beállítások',
    'pluginOptions' => [
        'expiryDays' => 365,
        'hasTransition' => false,
        'revokeBtn' => '<div class="cc-revoke {{classes}}">Cookie Policy</div>',
    ],
]);

Add the cookie settings form to any of your controller:

<?php

namespace app\controllers;

class SiteController extends \yii\web\Controller
{
    public function actions()
    {
        return [
            'cookie-settings' => \albertborsos\cookieconsent\actions\CookieSettingsAction::class,
            'privacy-policy' => \albertborsos\cookieconsent\actions\PrivacyPolicyAction::class,
        ];
    }
}

Check your relevant widget is allowed by the user or not with the CookieConsent helper class in the following way:

<?php

use \albertborsos\cookieconsent\helpers\CookieHelper;
use \albertborsos\cookieconsent\Component;

if(CookieHelper::isAllowedType(CookieHelper::TYPE_GOOGLE_ANALYTICS)){
    // register GA script
}

if(CookieHelper::isAllowedCategory(CookieHelper::CATEGORY_BEHAVIOR)){
    // register hotjar script
}

yii2-gdpr-cookie-consent's People

Contributors

albertborsos avatar xavsio4 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.