Class yii\bootstrap4\Alert

Inheritanceyii\bootstrap4\Alert » yii\bootstrap4\Widget » yii\base\Widget
Uses Traitsyii\bootstrap4\BootstrapWidgetTrait
Source Code https://github.com/yiisoft/yii2-bootstrap4/blob/master/src/Alert.php

Alert renders an alert bootstrap component.

For example,

echo Alert::widget([
    'options' => [
        'class' => 'alert-info',
    ],
    'body' => 'Say hello...',
]);

The following example will show the content enclosed between the begin() and end() calls within the alert box:

Alert::begin([
    'options' => [
        'class' => 'alert-warning',
    ],
]);

echo 'Say hello...';

Alert::end();

See also https://getbootstrap.com/docs/4.5/components/alerts/.

Public Properties

Hide inherited properties

Property Type Description Defined By
$body string The body content in the alert component. yii\bootstrap4\Alert
$clientEvents array The event handlers for the underlying Bootstrap JS plugin. yii\bootstrap4\BootstrapWidgetTrait
$clientOptions array The options for the underlying Bootstrap JS plugin. yii\bootstrap4\BootstrapWidgetTrait
$closeButton array|false The options for rendering the close button tag. yii\bootstrap4\Alert
$options array The HTML attributes for the widget container tag. yii\bootstrap4\Widget

Protected Methods

Hide inherited methods

Method Description Defined By
initOptions() Initializes the widget options. yii\bootstrap4\Alert
registerClientEvents() Registers JS event handlers that are listed in $clientEvents. yii\bootstrap4\BootstrapWidgetTrait
registerPlugin() Registers a specific Bootstrap plugin and the related events yii\bootstrap4\BootstrapWidgetTrait
renderBodyEnd() Renders the alert body and the close button (if any). yii\bootstrap4\Alert
renderCloseButton() Renders the close button. yii\bootstrap4\Alert

Property Details

Hide inherited properties

$body public property

The body content in the alert component. Note that anything between the begin() and end() calls of the Alert widget will also be treated as the body content, and will be rendered before this.

public string $body null
$closeButton public property

The options for rendering the close button tag. The close button is displayed in the header of the modal window. Clicking on the button will hide the modal window. If this is false, no close button will be rendered.

The following special options are supported:

  • tag: string, the tag name of the button. Defaults to 'button'.
  • label: string, the label of the button. Defaults to '×'.

The rest of the options will be rendered as the HTML attributes of the button tag. Please refer to the Alert documentation for the supported HTML attributes.

public array|false $closeButton = []

Method Details

Hide inherited methods

getView() public abstract method

Defined in: yii\bootstrap4\BootstrapWidgetTrait::getView()

See also \yii\base\Widget::getView().

public abstract \yii\web\View getView ( )
return \yii\web\View

The view object that can be used to render views or view files.

                abstract function getView();

            
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 ( )

                public function init()
{
    parent::init();
    $this->initOptions();
    echo Html::beginTag('div', $this->options) . "\n";
}

            
initOptions() protected method

Initializes the widget options.

This method sets the default values for various options.

protected void initOptions ( )

                protected function initOptions()
{
    Html::addCssClass($this->options, ['widget' => 'alert']);
    if ($this->closeButton !== false) {
        $this->closeButton = array_merge([
            'data-dismiss' => 'alert',
            'class' => ['widget' => 'close'],
        ], $this->closeButton);
        Html::addCssClass($this->options, ['toggle' => 'alert-dismissible']);
    }
    if (!isset($this->options['role'])) {
        $this->options['role'] = 'alert';
    }
}

            
registerClientEvents() protected method

Defined in: yii\bootstrap4\BootstrapWidgetTrait::registerClientEvents()

Registers JS event handlers that are listed in $clientEvents.

protected void registerClientEvents ( )

                protected function registerClientEvents()
{
    if (!empty($this->clientEvents)) {
        $id = $this->options['id'];
        $js = [];
        foreach ($this->clientEvents as $event => $handler) {
            $js[] = "jQuery('#$id').on('$event', $handler);";
        }
        $this->getView()->registerJs(implode("\n", $js));
    }
}

            
registerPlugin() protected method

Defined in: yii\bootstrap4\BootstrapWidgetTrait::registerPlugin()

Registers a specific Bootstrap plugin and the related events

protected void registerPlugin ( $name )
$name string

The name of the Bootstrap plugin

                protected function registerPlugin($name)
{
    $view = $this->getView();
    BootstrapPluginAsset::register($view);
    $id = $this->options['id'];
    if ($this->clientOptions !== false) {
        $options = empty($this->clientOptions) ? '' : Json::htmlEncode($this->clientOptions);
        $js = "jQuery('#$id').$name($options);";
        $view->registerJs($js);
    }
    $this->registerClientEvents();
}

            
renderBodyEnd() protected method

Renders the alert body and the close button (if any).

protected string renderBodyEnd ( )
return string

The rendering result

                protected function renderBodyEnd()
{
    return $this->body . "\n" .  $this->renderCloseButton() . "\n";
}

            
renderCloseButton() protected method

Renders the close button.

protected string renderCloseButton ( )
return string

The rendering result

                protected function renderCloseButton()
{
    if (($closeButton = $this->closeButton) !== false) {
        $tag = ArrayHelper::remove($closeButton, 'tag', 'button');
        $label = ArrayHelper::remove($closeButton, 'label', Html::tag('span', '×', [
            'aria-hidden' => 'true'
        ]));
        if ($tag === 'button' && !isset($closeButton['type'])) {
            $closeButton['type'] = 'button';
        }
        return Html::tag($tag, $label, $closeButton);
    } else {
        return null;
    }
}

            
run() public method

public void run ( )

                public function run()
{
    echo "\n" . $this->renderBodyEnd();
    echo "\n" . Html::endTag('div');
    $this->registerPlugin('alert');
}