Skip to content

Commit a4e8114

Browse files
committed
Fixed Alert widget
1 parent 1d51cb3 commit a4e8114

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

widgets/Alert.php

+23-24
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class Alert extends \yii\bootstrap\Widget
2727
/**
2828
* @var array the alert types configuration for the flash messages.
2929
* This array is setup as $key => $value, where:
30-
* - $key is the name of the session flash variable
31-
* - $value is the bootstrap alert type (i.e. danger, success, info, warning)
30+
* - key: the name of the session flash variable
31+
* - value: the bootstrap alert type (i.e. danger, success, info, warning)
3232
*/
3333
public $alertTypes = [
3434
'error' => 'alert-danger',
@@ -39,37 +39,36 @@ class Alert extends \yii\bootstrap\Widget
3939
];
4040
/**
4141
* @var array the options for rendering the close button tag.
42+
* Array will be passed to [[\yii\bootstrap\Alert::closeButton]].
4243
*/
4344
public $closeButton = [];
4445

45-
46-
public function init()
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function run()
4750
{
48-
parent::init();
49-
5051
$session = Yii::$app->session;
5152
$flashes = $session->getAllFlashes();
52-
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
53+
$appendClass = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
5354

54-
foreach ($flashes as $type => $data) {
55-
if (isset($this->alertTypes[$type])) {
56-
$data = (array) $data;
57-
foreach ($data as $i => $message) {
58-
/* initialize css class for each alert box */
59-
$this->options['class'] = $this->alertTypes[$type] . $appendCss;
60-
61-
/* assign unique id to each alert box */
62-
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i;
63-
64-
echo \yii\bootstrap\Alert::widget([
65-
'body' => $message,
66-
'closeButton' => $this->closeButton,
67-
'options' => $this->options,
68-
]);
69-
}
55+
foreach ($flashes as $type => $flash) {
56+
if (!isset($this->alertTypes[$type])) {
57+
continue;
58+
}
7059

71-
$session->removeFlash($type);
60+
foreach ((array) $flash as $i => $message) {
61+
echo \yii\bootstrap\Alert::widget([
62+
'body' => $message,
63+
'closeButton' => $this->closeButton,
64+
'options' => array_merge($this->options, [
65+
'id' => $this->getId() . '-' . $type . '-' . $i,
66+
'class' => $this->alertTypes[$type] . $appendClass,
67+
]),
68+
]);
7269
}
70+
71+
$session->removeFlash($type);
7372
}
7473
}
7574
}

0 commit comments

Comments
 (0)