Skip to content

Commit

Permalink
Alert types for flash messages are now configurable.
Browse files Browse the repository at this point in the history
Each flash variable can be mapped to a bootstrap alert type through a configuration array passed to this widget.
  • Loading branch information
kartik-v committed Nov 20, 2013
1 parent 8ff4c3d commit 2bff5f8
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions apps/advanced/frontend/widgets/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@
class Alert extends \yii\bootstrap\Widget
{
/**
* @var array the allowed bootstrap alert types.
* @var array the alert types configuration for the flash messages.
* This array is setup as $key => $value, where:
* - $key is the name of the session flash variable
* - $value is the bootstrap alert type (i.e. danger, success, info, warning)
*/
public $allowedTypes = ['error', 'danger', 'success', 'info', 'warning'];
public $alertTypes = [
'error' => 'danger',
'danger' => 'danger',
'success' => 'success',
'info' => 'info',
'warning' => 'warning'
];

/**
* @var array the options for rendering the close button tag.
Expand All @@ -37,15 +46,13 @@ public function init()
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';

foreach ($flashes as $type => $message) {
if (in_array($type, $this->allowedTypes)) {
$this->options['class'] = (($type === 'error') ? 'alert-danger' : 'alert-' . $type) . $appendCss;
echo \yii\bootstrap\Alert::widget([
'body' => $message,
'closeButton' => $this->closeButton,
'options' => $this->options
]);
$session->removeFlash($type);
}
$this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss;
echo \yii\bootstrap\Alert::widget([
'body' => $message,
'closeButton' => $this->closeButton,
'options' => $this->options
]);
$session->removeFlash($type);
}
parent::init();
}
Expand Down

0 comments on commit 2bff5f8

Please sign in to comment.