Fixed Alert widget

This commit is contained in:
SilverFire - Dmitry Naumenko 2017-08-09 20:58:58 +03:00
parent 1d51cb3790
commit a4e8114f60

View File

@ -27,8 +27,8 @@ class Alert extends \yii\bootstrap\Widget
/** /**
* @var array the alert types configuration for the flash messages. * @var array the alert types configuration for the flash messages.
* This array is setup as $key => $value, where: * This array is setup as $key => $value, where:
* - $key is the name of the session flash variable * - key: the name of the session flash variable
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) * - value: the bootstrap alert type (i.e. danger, success, info, warning)
*/ */
public $alertTypes = [ public $alertTypes = [
'error' => 'alert-danger', 'error' => 'alert-danger',
@ -39,37 +39,36 @@ class Alert extends \yii\bootstrap\Widget
]; ];
/** /**
* @var array the options for rendering the close button tag. * @var array the options for rendering the close button tag.
* Array will be passed to [[\yii\bootstrap\Alert::closeButton]].
*/ */
public $closeButton = []; public $closeButton = [];
/**
public function init() * {@inheritdoc}
*/
public function run()
{ {
parent::init();
$session = Yii::$app->session; $session = Yii::$app->session;
$flashes = $session->getAllFlashes(); $flashes = $session->getAllFlashes();
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; $appendClass = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
foreach ($flashes as $type => $data) { foreach ($flashes as $type => $flash) {
if (isset($this->alertTypes[$type])) { if (!isset($this->alertTypes[$type])) {
$data = (array) $data; continue;
foreach ($data as $i => $message) { }
/* initialize css class for each alert box */
$this->options['class'] = $this->alertTypes[$type] . $appendCss;
/* assign unique id to each alert box */
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i;
foreach ((array) $flash as $i => $message) {
echo \yii\bootstrap\Alert::widget([ echo \yii\bootstrap\Alert::widget([
'body' => $message, 'body' => $message,
'closeButton' => $this->closeButton, 'closeButton' => $this->closeButton,
'options' => $this->options, 'options' => array_merge($this->options, [
'id' => $this->getId() . '-' . $type . '-' . $i,
'class' => $this->alertTypes[$type] . $appendClass,
]),
]); ]);
} }
$session->removeFlash($type); $session->removeFlash($type);
} }
} }
}
} }