Ethan C
Ethan C

Reputation: 1518

Yii2 ActiveFormAsset dependencies

I'm foregoing Yii2's bundled jQuery and Bootstrap assets in exchange for ones I've bundled myself using npm/browserify So step one was to remove jQuery and Bootstrap from yii\web\YiiAsset via the config:

'components' => [
        'assetManager' => [
            'bundles' => [
                'yii\web\JqueryAsset' => [
                    'js'=>[]
                ],
                'yii\bootstrap\BootstrapPluginAsset' => [
                    'js'=>[]
                ],
                'yii\bootstrap\BootstrapAsset' => [
                    'css' => [],
                ],

            ],
        ],
...
]

I want to put this in the footer, obviously, but it needs to load before any other assets so that jQuery is available to them.

Here's my AssetBundle:

class AppAssets extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';

    public $depends = [
        'app\assets\CustomAssets',
        'yii\web\YiiAsset',
    ];
}

With my CustomAssets bundle (that contains my own JS and CSS) declared first in depends, I think this would work, but I also have an ActiveForm on the page, and this registers its own asset bundle, which is dependent on the various Yii assets. Here's where I embed that form:

<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
            ...
<?php ActiveForm::end(); ?>

Is there a way I can override the depends on ActiveFormAsset so that it also depends on my scripts?

Upvotes: 0

Views: 679

Answers (2)

e-frank
e-frank

Reputation: 749

what i did was replacing shipped assets, instead of killing them:

    'bundles'            => [
        'yii\bootstrap\BootstrapAsset'       => ['class' => 'common\assets\BootstrapAsset',],
        'yii\bootstrap\BootstrapPluginAsset' => ['class' => 'common\assets\BootstrapPluginAsset',],
    ],            

Upvotes: 0

Ethan C
Ethan C

Reputation: 1518

Since asset bundle dependencies are transitive, it occurred to me that in the same way I'm overriding the config for the Jquery and Bootstrap bundles, I can override the config for yii\web\YiiAsset like so:

'components' => [
    'assetManager' => [
        'bundles' => [
            ...
            'yii\web\YiiAsset' => [
                'depends' => [
                    'app\assets\CustomAssets',
                ],
             ],
        ],
    ],
    ...
]

So when ActiveFormAsset tries to load yii\web\YiiAsset, it processes the dependency on my own scripts/styles, and also covers any other assets that get loaded and might depend on yii\web\YiiAsset.

Upvotes: 0

Related Questions