AlertBlock Widget AlertBlock.php

Thankful to Krajee!
to get more out of us.

NOTE: This extension depends on the yiisoft/yii2-bootstrap extension. Check the composer.json for this extension's requirements and dependencies that may be updated by composer.

The Alert Block widget provides ability to groups multiple \kartik\widget\Alert or kartik\widget\Growl widgets in a single block. The widget by default stacks the alert messages vertically on the current page. The widget offers the following features:

  • Session Flashes: The widget automatically reads the messages stored in session flash via Yii::$app->session->setFlash(). All the flash messages stored for the session are displayed and removed from the session.
  • Custom Stacked Alerts: You can override the useSessionFlash setting to false and display your own alert messages in a single block.
  • Alert Widget Types: You can choose the TYPE_ALERT style or the TYPE_GROWL to choose the widget for triggering your alert notifications.

Alert Block Demo (Configure)


 

use kartik\widgets\AlertBlock;
/**
 * You can place either of the codes below in your view layout
 * file to automatically notify flash messages set through
 * `Yii::$app()->session->setFlash();`
 */

// with alert
echo AlertBlock::widget([
    'useSessionFlash' => true,
    'type' => AlertBlock::TYPE_ALERT
]);

// with growl
echo AlertBlock::widget([
    'useSessionFlash' => true,
    'type' => AlertBlock::TYPE_GROWL
]);

The alert block widget can be placed on your view or view layout to automatically render all flash messages or any custom blocks of alert notifications. The parameter settings available for this widget are:

  • type: string the type of the alert block widget. Defaults to 'alert' or AlertBlock::TYPE_ALERT. You can select one of the following options:

    • AlertBlock::TYPE_ALERT or 'alert': Will use \kartik\widgets\Alert widget to render alerts..

    • AlertBlock::TYPE_GROWL or 'growl': Will use \kartik\widgets\Growl widget to render alerts..

  • delay: integer|boolean time in milliseconds after which each alert displayed fades out. If set to 0 or false, alerts will never fade out and will be displayed forever. This defaults to 2000, when the type is AlertBlock::TYPE_ALERT and 1000, when the type is AlertBlock::TYPE_GROWL.

  • useSessionFlash: boolean whether to automatically use messages set via Yii::$app->session->setFlash(). Defaults to true. If set to false, you would need to pass the body setting within alertSetting array.

  • alertSettings: array the alert types configuration for the alert messages. This array is setup as $alert => $settings, where:

    • $alert: string is the name of the message or session flash variable (e.g. error, success, info, warning). If you set useSessionFlash to false, you can provide any value here.

    • $settings: array, the \kartik\widgets\Alert or \kartik\widgets\Growl widget settings depending on what type has been set.

  • closeButton: array the options for displaying the dismiss or closeButton. This parameter is to be set similar to the closeButton option in Yii Bootstrap Alert Widget. This is overridden by the closeButton setting for each alert that can be set in alertSettings.

  • options: array the HTML attributes for the alert block container.