Add support for stacking Flash messages

See Issue #7830
This commit is contained in:
xhs345 2016-10-27 14:39:21 -07:00
parent 135a24e0f1
commit c59fb85da8
3 changed files with 52 additions and 29 deletions

View file

@ -82,12 +82,18 @@ class FlashComponent extends Component {
} }
$options['element'] = $plugin . 'Flash/' . $element; $options['element'] = $plugin . 'Flash/' . $element;
CakeSession::write('Message.' . $options['key'], array( $messages = CakeSession::read('Message.' . $options['key']);
$newMessage = array(
'message' => $message, 'message' => $message,
'key' => $options['key'], 'key' => $options['key'],
'element' => $options['element'], 'element' => $options['element'],
'params' => $options['params'] 'params' => $options['params']
)); );
$messages[] = $newMessage;
CakeSession::write('Message.' . $options['key'], $messages);
} }
/** /**

View file

@ -59,10 +59,12 @@ class FlashComponentTest extends CakeTestCase {
$this->Flash->set('This is a test message'); $this->Flash->set('This is a test message');
$expected = array( $expected = array(
array(
'message' => 'This is a test message', 'message' => 'This is a test message',
'key' => 'flash', 'key' => 'flash',
'element' => 'Flash/default', 'element' => 'Flash/default',
'params' => array() 'params' => array()
)
); );
$result = CakeSession::read('Message.flash'); $result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
@ -72,30 +74,36 @@ class FlashComponentTest extends CakeTestCase {
'params' => array('foo' => 'bar') 'params' => array('foo' => 'bar')
)); ));
$expected = array( $expected = array(
array(
'message' => 'This is a test message', 'message' => 'This is a test message',
'key' => 'flash', 'key' => 'flash',
'element' => 'Flash/test', 'element' => 'Flash/test',
'params' => array('foo' => 'bar') 'params' => array('foo' => 'bar')
)
); );
$result = CakeSession::read('Message.flash'); $result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->Flash->set('This is a test message', array('element' => 'MyPlugin.alert')); $this->Flash->set('This is a test message', array('element' => 'MyPlugin.alert'));
$expected = array( $expected = array(
array(
'message' => 'This is a test message', 'message' => 'This is a test message',
'key' => 'flash', 'key' => 'flash',
'element' => 'MyPlugin.Flash/alert', 'element' => 'MyPlugin.Flash/alert',
'params' => array() 'params' => array()
)
); );
$result = CakeSession::read('Message.flash'); $result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
$this->Flash->set('This is a test message', array('key' => 'foobar')); $this->Flash->set('This is a test message', array('key' => 'foobar'));
$expected = array( $expected = array(
array(
'message' => 'This is a test message', 'message' => 'This is a test message',
'key' => 'foobar', 'key' => 'foobar',
'element' => 'Flash/default', 'element' => 'Flash/default',
'params' => array() 'params' => array()
)
); );
$result = CakeSession::read('Message.foobar'); $result = CakeSession::read('Message.foobar');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
@ -111,10 +119,12 @@ class FlashComponentTest extends CakeTestCase {
$this->Flash->set(new Exception('This is a test message', 404)); $this->Flash->set(new Exception('This is a test message', 404));
$expected = array( $expected = array(
array(
'message' => 'This is a test message', 'message' => 'This is a test message',
'key' => 'flash', 'key' => 'flash',
'element' => 'Flash/default', 'element' => 'Flash/default',
'params' => array('code' => 404) 'params' => array('code' => 404)
)
); );
$result = CakeSession::read('Message.flash'); $result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
@ -131,10 +141,12 @@ class FlashComponentTest extends CakeTestCase {
$FlashWithSettings = $this->Components->load('Flash', array('element' => 'test')); $FlashWithSettings = $this->Components->load('Flash', array('element' => 'test'));
$FlashWithSettings->set('This is a test message'); $FlashWithSettings->set('This is a test message');
$expected = array( $expected = array(
array(
'message' => 'This is a test message', 'message' => 'This is a test message',
'key' => 'flash', 'key' => 'flash',
'element' => 'Flash/test', 'element' => 'Flash/test',
'params' => array() 'params' => array()
)
); );
$result = CakeSession::read('Message.flash'); $result = CakeSession::read('Message.flash');
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);

View file

@ -82,10 +82,15 @@ class FlashHelper extends AppHelper {
)); ));
} }
$flash = $options + $flash;
CakeSession::delete("Message.$key"); 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;
} }
} }