Revert "[TASK] Introduce DeprecationUtility and move methods"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * View helper which renders the flash messages (if there are any) as an unsorted list.
27 *
28 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
29 *
30 *
31 * = Examples =
32 *
33 * <code title="Simple">
34 * <f:flashMessages />
35 * </code>
36 * <output>
37 * An ul-list of flash messages.
38 * </output>
39 *
40 * <code title="Output with custom css class">
41 * <f:flashMessages class="specialClass" />
42 * </code>
43 * <output>
44 * <ul class="specialClass">
45 * ...
46 * </ul>
47 * </output>
48 *
49 * <code title="TYPO3 core style">
50 * <f:flashMessages />
51 * </code>
52 * <output>
53 * <ul class="typo3-messages">
54 * <li class="alert alert-ok">
55 * <h4>Some Message Header</h4>
56 * Some message body
57 * </li>
58 * <li class="alert alert-notice">
59 * Some notice message without header
60 * </li>
61 * </ul>
62 * </output>
63 * <code title="Output flash messages as a description list">
64 * <f:flashMessages as="flashMessages">
65 * <dl class="messages">
66 * <f:for each="{flashMessages}" as="flashMessage">
67 * <dt>{flashMessage.code}</dt>
68 * <dd>{flashMessage.message}</dd>
69 * </f:for>
70 * </dl>
71 * </f:flashMessages>
72 * </code>
73 * <output>
74 * <dl class="messages">
75 * <dt>1013</dt>
76 * <dd>Some Warning Message.</dd>
77 * </dl>
78 * </output>
79 *
80 * <code title="Using a specific queue">
81 * <f:flashMessages queueIdentifier="myQueue" />
82 * </code>
83 *
84 * @api
85 */
86 class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
87 {
88 const RENDER_MODE_UL = 'ul';
89 const RENDER_MODE_DIV = 'div';
90
91 /**
92 * @var string
93 */
94 protected $tagName = 'ul';
95
96 /**
97 * Initialize arguments
98 *
99 * @return void
100 * @api
101 */
102 public function initializeArguments()
103 {
104 $this->registerUniversalTagAttributes();
105 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
106 }
107
108 /**
109 * Renders FlashMessages and flushes the FlashMessage queue
110 * Note: This disables the current page cache in order to prevent FlashMessage output
111 * from being cached.
112 *
113 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
114 * @param string $renderMode @deprecated since TYPO3 CMS 7.3. If you need custom output, use <f:flashMessages as="messages"><f:for each="messages" as="message">...</f:for></f:flashMessages>
115 * @param string $as The name of the current flashMessage variable for rendering inside
116 * @return string rendered Flash Messages, if there are any.
117 * @api
118 */
119 public function render($renderMode = null, $as = null)
120 {
121 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
122 $flashMessages = $this->controllerContext->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
123 if ($flashMessages === null || count($flashMessages) === 0) {
124 return '';
125 }
126 if ($renderMode !== null) {
127 GeneralUtility::deprecationLog('renderMode has been deprecated in TYPO3 CMS 7.3 and will be removed in TYPO3 CMS 8');
128 if ($renderMode === self::RENDER_MODE_DIV) {
129 $content = $this->renderDiv($flashMessages);
130 } else {
131 $content = $this->renderAsList($flashMessages);
132 }
133 } else {
134 if ($as === null) {
135 $content = $this->renderAsList($flashMessages);
136 } else {
137 $content = $this->renderFromTemplate($flashMessages, $as);
138 }
139 }
140
141 return $content;
142 }
143
144 /**
145 * Renders the flash messages as unordered list
146 *
147 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
148 * @return string
149 */
150 protected function renderAsList(array $flashMessages)
151 {
152 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
153 $tagContent = '';
154 $this->tag->addAttribute('class', $flashMessagesClass);
155 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
156 foreach ($flashMessages as $singleFlashMessage) {
157 $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
158 $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
159 if ($singleFlashMessage->getTitle() !== '') {
160 $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
161 }
162 $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
163 }
164 $this->tag->setContent($tagContent);
165 return $this->tag->render();
166 }
167
168 /**
169 * Renders the flash messages as nested divs
170 * Defer the rendering of Flash Messages to the template. In this case,
171 * the flash messages are stored in the template inside the variable specified
172 * in "as".
173 *
174 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
175 * @param string $as
176 * @return string
177 */
178 protected function renderFromTemplate(array $flashMessages, $as)
179 {
180 $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
181 $templateVariableContainer->add($as, $flashMessages);
182 $content = $this->renderChildren();
183 $templateVariableContainer->remove($as);
184
185 return $content;
186 }
187
188 /**
189 * Renders the flash messages as nested divs
190 *
191 * @deprecated in 7.3 will be removed in 8.0
192 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
193 * @return string
194 */
195 protected function renderDiv(array $flashMessages)
196 {
197 $this->tag->setTagName('div');
198 if ($this->hasArgument('class')) {
199 $this->tag->addAttribute('class', $this->arguments['class']);
200 } else {
201 $this->tag->addAttribute('class', 'typo3-messages');
202 }
203 $tagContent = '';
204 foreach ($flashMessages as $singleFlashMessage) {
205 $tagContent .= $singleFlashMessage->render();
206 }
207 $this->tag->setContent($tagContent);
208 return $this->tag->render();
209 }
210 }