Skip to content
PHP Ecosystem
Explore

BootstrapWidgetTrait

API reference › Bootstrap 6 › BootstrapWidgetTrait

traityii2\extensions\bootstrap6\BootstrapWidgetTrait
Sourcesrc/BootstrapWidgetTrait.php
Used by 2 types

Provides Bootstrap plugin registration, client options, and client events for widgets.

The using class must declare a public $options array; the trait fills $options['id'] when it is not set. Plugins initialize through a dynamic import() of the same module URL that BootstrapPluginAsset registers, so the browser evaluates the bundle once and no global is required.

PropertyTypeDescriptionDefined by
$clientEventsarray<string, yii\web\JsExpression|string>Event handlers attached to the widget element, as event name => JavaScript handler source. Default: [].BootstrapWidgetTrait
$clientOptionsarray<string, mixed>|falseOptions passed to the plugin constructor, or false to skip the JavaScript initialization. Default: [].BootstrapWidgetTrait
MethodDescriptionDefined by
init()Initializes the widget and assigns an id to $options when it is not set.BootstrapWidgetTrait
pluginInitialization() protectedReturns the JavaScript that initializes a plugin with its normalized constructor options.BootstrapWidgetTrait
registerClientEvents() protectedRegisters the JavaScript event handlers listed in $clientEvents at View::POS_END.BootstrapWidgetTrait
registerPlugin() protectedRegisters the plugin asset, the plugin initialization when needed, and the $clientEvents.BootstrapWidgetTrait
public array<string, yii\web\JsExpression|string> $clientEvents = []

Event handlers attached to the widget element, as event name => JavaScript handler source.

For example, ['shown.bs.toast' => 'function (event) { ... }'].

public array<string, mixed>|false $clientOptions = []

Options passed to the plugin constructor, or false to skip the JavaScript initialization.

Enum cases at any depth are encoded as their value (backed) or name (pure), and Stringable values as their string; JsExpression values are emitted as raw JavaScript.

public function init(): void

Initializes the widget and assigns an id to $options when it is not set.

protected function pluginInitialization(string $name, string $element, string $options): string

Returns the JavaScript that initializes a plugin with its normalized constructor options.

Parameters

ParameterTypeDescription
$namestringPlugin class exported by the bundle.
$elementstringJavaScript expression selecting the plugin element.
$optionsstringJSON-encoded constructor options.

Returns

string

protected function registerClientEvents(): void

Registers the JavaScript event handlers listed in $clientEvents at View::POS_END.

protected function registerPlugin(string $name, bool $always = false): void

Registers the plugin asset, the plugin initialization when needed, and the $clientEvents.

The initialization is emitted when $clientOptions is a non-empty array, or when $always is true and $clientOptions is not false.

Parameters

ParameterTypeDescription
$namestringPlugin class exported by the bundle, for example Toast.
$alwaysboolWhether to initialize the plugin even when $clientOptions is empty.

Throws