@@ -27,8 +27,8 @@ class Alert extends \yii\bootstrap\Widget
27
27
/**
28
28
* @var array the alert types configuration for the flash messages.
29
29
* 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)
32
32
*/
33
33
public $ alertTypes = [
34
34
'error ' => 'alert-danger ' ,
@@ -39,37 +39,36 @@ class Alert extends \yii\bootstrap\Widget
39
39
];
40
40
/**
41
41
* @var array the options for rendering the close button tag.
42
+ * Array will be passed to [[\yii\bootstrap\Alert::closeButton]].
42
43
*/
43
44
public $ closeButton = [];
44
45
45
-
46
- public function init ()
46
+ /**
47
+ * {@inheritdoc}
48
+ */
49
+ public function run ()
47
50
{
48
- parent ::init ();
49
-
50
51
$ session = Yii::$ app ->session ;
51
52
$ flashes = $ session ->getAllFlashes ();
52
- $ appendCss = isset ($ this ->options ['class ' ]) ? ' ' . $ this ->options ['class ' ] : '' ;
53
+ $ appendClass = isset ($ this ->options ['class ' ]) ? ' ' . $ this ->options ['class ' ] : '' ;
53
54
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
+ }
70
59
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
+ ]);
72
69
}
70
+
71
+ $ session ->removeFlash ($ type );
73
72
}
74
73
}
75
74
}
0 commit comments