Skip to content

Commit

Permalink
Fix: Bootstrap theme loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Mtangoo committed Sep 29, 2024
1 parent 2089896 commit 8f4c837
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yii2\Extensions\Select2;

use Yii2\Extensions\Select2\Themes\Select2BS5ThemeAsset;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected function registerPlugin($name)
if (!isset($options['theme'])) {
//Do we have BS5?
if (class_exists('Yii2\Asset\BootstrapAsset')) {
$view->registerCssFile('@npm/select2-bootstrap-5-theme/dist/select2-bootstrap-5-theme.min.css');
Select2BS5ThemeAsset::register($view);
$options['theme'] = 'bootstrap-5';
} else {
$options['theme'] = 'classic';
Expand Down
31 changes: 31 additions & 0 deletions src/Themes/Select2BS5ThemeAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Yii2\Extensions\Select2\Themes;

use yii\web\AssetBundle;


class Select2BS5ThemeAsset extends AssetBundle
{
public $sourcePath = '@npm/select2-bootstrap-5-theme/dist';

public $js = [];

public $css = [
'select2-bootstrap-5-theme.min.css',
];

public function init()
{
parent::init();

//which BS version is installed (from highest to lowest)
if (class_exists('yii\bootstrap5\BootstrapAsset')) { //BS5 official
$this->depends[] = 'yii\bootstrap5\BootstrapAsset';
} else if (class_exists('Yii2\Asset\BootstrapAsset')) { //BS5 Yii2-Ext
$this->depends[] = 'Yii2\Asset\BootstrapAsset';
}
}
}

0 comments on commit 8f4c837

Please sign in to comment.