Trait yii\bootstrap5\BootstrapWidgetTrait

Implemented byyii\bootstrap5\Accordion, yii\bootstrap5\Alert, yii\bootstrap5\Breadcrumbs, yii\bootstrap5\Button, yii\bootstrap5\ButtonDropdown, yii\bootstrap5\ButtonGroup, yii\bootstrap5\ButtonToolbar, yii\bootstrap5\Carousel, yii\bootstrap5\Dropdown, yii\bootstrap5\InputWidget, yii\bootstrap5\LinkPager, yii\bootstrap5\Modal, yii\bootstrap5\Nav, yii\bootstrap5\NavBar, yii\bootstrap5\Offcanvas, yii\bootstrap5\Popover, yii\bootstrap5\Progress, yii\bootstrap5\Tabs, yii\bootstrap5\Toast, yii\bootstrap5\ToggleButtonGroup, yii\bootstrap5\Widget
Source Code https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/BootstrapWidgetTrait.php

BootstrapWidgetTrait is the trait, which provides basic for all Bootstrap widgets features.

Note: class, which uses this trait must declare public field named options with the array default value:

class MyWidget extends \yii\base\Widget
{
    use BootstrapWidgetTrait;

    public $options = [];
}

This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict.

Public Properties

Hide inherited properties

Property Type Description Defined By
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap5\BootstrapWidgetTrait
$clientOptions array|false The options for the underlying Bootstrap JS plugin/component. yii\bootstrap5\BootstrapWidgetTrait

Public Methods

Hide inherited methods

Method Description Defined By
init() Initializes the widget. yii\bootstrap5\BootstrapWidgetTrait

Protected Methods

Hide inherited methods

Method Description Defined By
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap5\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin/component and the related events. yii\bootstrap5\BootstrapWidgetTrait

Property Details

Hide inherited properties

$clientEvents public property

The event handlers for the underlying Bootstrap JS plugin. Please refer to the corresponding Bootstrap plugin Web page for possible events. For example, this page shows how to use the "Modal" plugin and the supported events (e.g. "shown.bs.modal").

public array $clientEvents = []
$clientOptions public property

The options for the underlying Bootstrap JS plugin/component. Please refer to the corresponding Bootstrap plugin/component Web page for possible options. For example, this page shows how to use the "Modal" component and the supported options (e.g. "backdrop"). If this property is false, registerJs() will not be called on the view to initialize the module.

Method Details

Hide inherited methods

init() public method

Initializes the widget.

This method will register the bootstrap asset bundle. If you override this method, make sure you call the parent implementation first.

public void init ( )
throws \yii\base\InvalidConfigException

                public function init(): void
{
    parent::init();
    if (!isset($this->options['id'])) {
        $this->options['id'] = $this->getId();
    }
}

            
registerClientEvents() protected method

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( ?string $name null )
$name ?string

                protected function registerClientEvents(?string $name = null): void
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        $appendix = ($name === 'dropdown') ? '.parentElement' : '';
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

Registers a specific Bootstrap plugin/component and the related events.

protected void registerPlugin ( string $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin(string $name): void
{
    /**
     * @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
     */
    $jsPlugins = [
        'alert',
        'button',
        'carousel',
        'collapse',
        'dropdown',
        'modal',
        'offcanvas',
        'popover',
        'scrollspy',
        'tab',
        'toast',
        'tooltip',
    ];
    if (in_array($name, $jsPlugins, true)) {
        $view = $this->getView();
        BootstrapPluginAsset::register($view);
        // 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
        if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
            $name = ucfirst($name);
            $id = $this->options['id'];
            $options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
            $view->registerJs("(new bootstrap.$name('#$id', $options));");
        }
        $this->registerClientEvents($name);
    }
}