Skip to content

Commit

Permalink
Add support for stacking Flash messages
Browse files Browse the repository at this point in the history
See Issue #7830
  • Loading branch information
xhs345 committed Oct 28, 2016
1 parent 135a24e commit c59fb85
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
10 changes: 8 additions & 2 deletions lib/Cake/Controller/Component/FlashComponent.php
Expand Up @@ -82,12 +82,18 @@ public function set($message, $options = array()) {
}
$options['element'] = $plugin . 'Flash/' . $element;

CakeSession::write('Message.' . $options['key'], array(
$messages = CakeSession::read('Message.' . $options['key']);

$newMessage = array(
'message' => $message,
'key' => $options['key'],
'element' => $options['element'],
'params' => $options['params']
));
);

$messages[] = $newMessage;

CakeSession::write('Message.' . $options['key'], $messages);
}

/**
Expand Down
60 changes: 36 additions & 24 deletions lib/Cake/Test/Case/Controller/Component/FlashComponentTest.php
Expand Up @@ -59,10 +59,12 @@ public function testSet() {

$this->Flash->set('This is a test message');
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array()
array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array()
)
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);
Expand All @@ -72,30 +74,36 @@ public function testSet() {
'params' => array('foo' => 'bar')
));
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/test',
'params' => array('foo' => 'bar')
array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/test',
'params' => array('foo' => 'bar')
)
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);

$this->Flash->set('This is a test message', array('element' => 'MyPlugin.alert'));
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'MyPlugin.Flash/alert',
'params' => array()
array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'MyPlugin.Flash/alert',
'params' => array()
)
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);

$this->Flash->set('This is a test message', array('key' => 'foobar'));
$expected = array(
'message' => 'This is a test message',
'key' => 'foobar',
'element' => 'Flash/default',
'params' => array()
array(
'message' => 'This is a test message',
'key' => 'foobar',
'element' => 'Flash/default',
'params' => array()
)
);
$result = CakeSession::read('Message.foobar');
$this->assertEquals($expected, $result);
Expand All @@ -111,10 +119,12 @@ public function testSetWithException() {

$this->Flash->set(new Exception('This is a test message', 404));
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array('code' => 404)
array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/default',
'params' => array('code' => 404)
)
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);
Expand All @@ -131,10 +141,12 @@ public function testSetWithComponentConfiguration() {
$FlashWithSettings = $this->Components->load('Flash', array('element' => 'test'));
$FlashWithSettings->set('This is a test message');
$expected = array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/test',
'params' => array()
array(
'message' => 'This is a test message',
'key' => 'flash',
'element' => 'Flash/test',
'params' => array()
)
);
$result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result);
Expand Down
11 changes: 8 additions & 3 deletions lib/Cake/View/Helper/FlashHelper.php
Expand Up @@ -82,10 +82,15 @@ public function render($key = 'flash', $options = array()) {
));
}

$flash = $options + $flash;
CakeSession::delete("Message.$key");
$flash['key'] = $key;

return $this->_View->element($flash['element'], $flash);
$out = '';
foreach ($flash as $message) {
$message['key'] = $key;
$message = $options + $message;
$out .= $this->_View->element($message['element'], $message);
}

return $out;
}
}

0 comments on commit c59fb85

Please sign in to comment.