1

I'm using the extension dmstr/yii2-cookie-consent for cookie consent and I want to modify the CSS of the extension. The CSS in the extension is included as AssetBundle.

Here are my questions:

  • How to overload the extension's CSS with my CSS during deleopment (no AssetBundle, no cahing)?
  • How to use my CSS in production, i.e. is it possible to permanently overwrite an extension's CSS (using caching of course)?

2 Answers 2

1

During development you can use

$this->registerCss(".your-css {}")

for prototyping

When going live you can overload the extensions asset bundle using dependency injection

'assetManager' => [
    'bundles' => [
        'dmstr\cookieconsent\assets\CookieConsentAsset' => [
            'sourcePath' => '/path/to/your/source/path',
            'css' => [
                'css/yourstyles.css'
            ]
        ]
    ]
]

Keep in mind that the package offers the functionality to load assets depending on the app env. In production mode a compressed version of the css is loaded.

https://github.com/dmstr/yii2-cookie-consent/blob/master/src/assets/CookieConsentAsset.php#L20

Sign up to request clarification or add additional context in comments.

2 Comments

When using depency injection, is my CSS going to be compressed, too?
Depends on your configuration. Have a look at this: yiiframework.com/doc/guide/2.0/en/…
1

Adding just as another option

If I am not wrong you are using the widget I would extend the function run() which is registering the scripts and create my own asset class file with my custom css file loaded.

I assume that you are using the advance-app for Yii2, or adjust the namespace accordingly.

The AssetBundle class

<?php

namespace common\assets;

use yii\web\AssetBundle;

class CookieConsentAsset extends AssetBundle
{

    public $sourcePath = __DIR__;

    public $css = [
        'path/to/custom.css',
    ];

    public $depends = [
        'dmstr\cookieconsent\assets\CookieConsentAsset'
    ];

}

and extend the widget class under common\components or common\widgets or whatever suits you

<?php
namespace common\components;

use dmstr\cookieconsent\widgets\CookieConsent as BaseCookieConsent;
use common\assets\CookieConsentAsset;

class CookieConsent extends BaseCookieConsent
{
    public function run(){
        CookieConsentAsset::register($this->view);
        parent::run();
    }
}

Now you can use your own widget by changing the namespace to

<?php
    use common\components\CookieConsent;

    echo CookieConsent::widget();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.