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+ /**
32+ * Used to render the message set in FlashComponent::set()
33+ *
34+ * In your view: $this->Flash->render('somekey');
35+ * Will default to flash if no param is passed
36+ *
37+ * You can pass additional information into the flash message generation. This allows you
38+ * to consolidate all the parameters for a given type of flash message into the view.
39+ *
40+ * ```
41+ * echo $this->Flash->render('flash', ['params' => ['name' => $user['User']['name']]]);
42+ * ```
43+ *
44+ * This would pass the current user's name into the flash message, so you could create personalized
45+ * messages without the controller needing access to that data.
46+ *
47+ * Lastly you can choose the element that is used for rendering the flash message. Using
48+ * custom elements allows you to fully customize how flash messages are generated.
49+ *
50+ * ```
51+ * echo $this->Flash->render('flash', ['element' => 'my_custom_element']);
52+ * ```
53+ *
54+ * If you want to use an element from a plugin for rendering your flash message
55+ * you can use the dot notation for the plugin's element name:
56+ *
57+ * ```
58+ * echo $this->Flash->render('flash', [
59+ * 'element' => 'MyPlugin.my_custom_element',
60+ * ]);
61+ * ```
62+ *
63+ * @param string $key The [Flash.]key you are rendering in the view.
64+ * @param array $options Additional options to use for the creation of this flash message.
65+ * Supports the 'params', and 'element' keys that are used in the helper.
66+ * @return string|void Rendered flash message or null if flash key does not exist
67+ * in session.
68+ * @throws \UnexpectedValueException If value for flash settings key is not an array.
69+ */
70+ public function render ($ key = 'flash ' , array $ options = []) {
71+ if (!$ this ->request ->session ()->check ("Flash. $ key " )) {
72+ return ;
73+ }
74+
75+ $ flash = $ this ->request ->session ()->read ("Flash. $ key " );
76+ if (!is_array ($ flash )) {
77+ throw new \UnexpectedValueException (sprintf (
78+ 'Value for flash setting key "%s" must be an array. ' ,
79+ $ key
80+ ));
81+ }
82+ $ flash = $ options + $ flash ;
83+ $ this ->request ->session ()->delete ("Flash. $ key " );
84+
85+ $ flash ['element ' ] = 'Bootstrap3. ' .$ flash ['element ' ] ;
86+
87+ return $ this ->_View ->element ($ flash ['element ' ], $ flash );
88+ }
89+
90+ }
91+
92+ ?>
0 commit comments