Skip to content

Commit 873a30d

Browse files
committed
Merge branch 'flash-helper'
2 parents 8a964d2 + 10b289e commit 873a30d

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$helper = new \Bootstrap3\View\Helper\BootstrapHtmlHelper ($this) ;
3+
echo $helper->alert (h($message), 'danger', $params) ;
4+
?>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$helper = new \Bootstrap3\View\Helper\BootstrapHtmlHelper ($this) ;
3+
echo $helper->alert (h($message), 'info', $params) ;
4+
?>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$helper = new \Bootstrap3\View\Helper\BootstrapHtmlHelper ($this) ;
3+
echo $helper->alert (h($message), 'success', $params) ;
4+
?>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
$helper = new \Bootstrap3\View\Helper\BootstrapHtmlHelper ($this) ;
3+
echo $helper->alert (h($message), 'warning', $params) ;
4+
?>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/**
4+
* Bootstrap Flash Helper
5+
*
6+
*
7+
* PHP 5
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
*
16+
* @copyright Copyright (c) Mikaël Capelle (http://mikael-capelle.fr)
17+
* @link http://mikael-capelle.fr
18+
* @package app.View.Helper
19+
* @since Apache v2
20+
* @license http://www.apache.org/licenses/LICENSE-2.0
21+
*/
22+
23+
namespace Bootstrap3\View\Helper;
24+
25+
use Cake\View\Helper\FlashHelper;
26+
27+
class BootstrapFlashHelper extends FlashHelper {
28+
29+
use BootstrapTrait ;
30+
31+
protected $_bootstrapTemplates = ['info', 'error', 'success', 'warning'] ;
32+
33+
/**
34+
* Used to render the message set in FlashComponent::set()
35+
*
36+
* In your view: $this->Flash->render('somekey');
37+
* Will default to flash if no param is passed
38+
*
39+
* You can pass additional information into the flash message generation. This allows you
40+
* to consolidate all the parameters for a given type of flash message into the view.
41+
*
42+
* ```
43+
* echo $this->Flash->render('flash', ['params' => ['name' => $user['User']['name']]]);
44+
* ```
45+
*
46+
* This would pass the current user's name into the flash message, so you could create personalized
47+
* messages without the controller needing access to that data.
48+
*
49+
* Lastly you can choose the element that is used for rendering the flash message. Using
50+
* custom elements allows you to fully customize how flash messages are generated.
51+
*
52+
* ```
53+
* echo $this->Flash->render('flash', ['element' => 'my_custom_element']);
54+
* ```
55+
*
56+
* If you want to use an element from a plugin for rendering your flash message
57+
* you can use the dot notation for the plugin's element name:
58+
*
59+
* ```
60+
* echo $this->Flash->render('flash', [
61+
* 'element' => 'MyPlugin.my_custom_element',
62+
* ]);
63+
* ```
64+
*
65+
* @param string $key The [Flash.]key you are rendering in the view.
66+
* @param array $options Additional options to use for the creation of this flash message.
67+
* Supports the 'params', and 'element' keys that are used in the helper.
68+
* @return string|void Rendered flash message or null if flash key does not exist
69+
* in session.
70+
* @throws \UnexpectedValueException If value for flash settings key is not an array.
71+
*/
72+
public function render($key = 'flash', array $options = []) {
73+
if (!$this->request->session()->check("Flash.$key")) {
74+
return;
75+
}
76+
77+
$flash = $this->request->session()->read("Flash.$key");
78+
if (!is_array($flash)) {
79+
throw new \UnexpectedValueException(sprintf(
80+
'Value for flash setting key "%s" must be an array.',
81+
$key
82+
));
83+
}
84+
$flash = $options + $flash;
85+
$this->request->session()->delete("Flash.$key");
86+
87+
$element = $flash['element'] ;
88+
if (in_array(basename($element), $this->_bootstrapTemplates)) {
89+
$flash['element'] = 'Bootstrap3.'.$element ;
90+
}
91+
92+
return $this->_View->element($flash['element'], $flash);
93+
}
94+
95+
}
96+
97+
?>

0 commit comments

Comments
 (0)