octicon-rss(16/)
You've already forked yii2
mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 02:13:17 +08:00
Merge pull request #1250 from kartik-v/patch-1
Update Alert.php to display multiple flash messages for the session.
This commit is contained in:
octicon-git-branch(16/)
octicon-tag(16/)
octicon-diff(16/tw-mr-1) 1 changed files with 41 additions and 23 deletions
@@ -7,42 +7,60 @@
|
||||
|
||||
namespace frontend\widgets;
|
||||
|
||||
use yii\helpers\Html;
|
||||
|
||||
/**
|
||||
* Alert widget renders a message from session flash. You can set message as following:
|
||||
* Alert widget renders a message from session flash. All flash messages are displayed
|
||||
* in the sequence they were assigned using setFlash. You can set message as following:
|
||||
*
|
||||
* - \Yii::$app->getSession()->setFlash('error', 'This is the message');
|
||||
* - \Yii::$app->getSession()->setFlash('success', 'This is the message');
|
||||
* - \Yii::$app->getSession()->setFlash('info', 'This is the message');
|
||||
*
|
||||
* @author Kartik Visweswaran <kartikv2@gmail.com>
|
||||
* @author Alexander Makarov <sam@rmcerative.ru>
|
||||
*/
|
||||
class Alert extends \yii\bootstrap\Alert
|
||||
class Alert extends \yii\bootstrap\Widget
|
||||
{
|
||||
private $_doNotRender = false;
|
||||
/**
|
||||
* @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 $alertTypes = [
|
||||
'error' => 'danger',
|
||||
'danger' => 'danger',
|
||||
'success' => 'success',
|
||||
'info' => 'info',
|
||||
'warning' => 'warning'
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array the options for rendering the close button tag.
|
||||
*/
|
||||
public $closeButton = [];
|
||||
|
||||
public function init()
|
||||
{
|
||||
if ($this->body = \Yii::$app->getSession()->getFlash('error', null, true)) {
|
||||
Html::addCssClass($this->options, 'alert-danger');
|
||||
} elseif ($this->body = \Yii::$app->getSession()->getFlash('success', null, true)) {
|
||||
Html::addCssClass($this->options, 'alert-success');
|
||||
} elseif ($this->body = \Yii::$app->getSession()->getFlash('info', null, true)) {
|
||||
Html::addCssClass($this->options, 'alert-info');
|
||||
} elseif ($this->body = \Yii::$app->getSession()->getFlash('warning', null, true)) {
|
||||
Html::addCssClass($this->options, 'alert-warning');
|
||||
} else {
|
||||
$this->_doNotRender = true;
|
||||
return;
|
||||
}
|
||||
|
||||
parent::init();
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
if (!$this->_doNotRender) {
|
||||
parent::run();
|
||||
$session = \Yii::$app->getSession();
|
||||
$flashes = $session->getAllFlashes();
|
||||
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
|
||||
|
||||
foreach ($flashes as $type => $message) {
|
||||
/* initialize css class for each alert box */
|
||||
$this->options['class'] = 'alert-' . $this->alertTypes[$type] . $appendCss;
|
||||
|
||||
/* assign unique id to each alert box */
|
||||
$this->options['id'] = $this->getId() . '-' . $type;
|
||||
|
||||
echo \yii\bootstrap\Alert::widget([
|
||||
'body' => $message,
|
||||
'closeButton' => $this->closeButton,
|
||||
'options' => $this->options
|
||||
]);
|
||||
|
||||
$session->removeFlash($type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user