[!!!][FEATURE] Switch to standalone Fluid template engine
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * View helper which renders the flash messages (if there are any) as an unsorted list.
19 *
20 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
21 *
22 *
23 * = Examples =
24 *
25 * <code title="Simple">
26 * <f:flashMessages />
27 * </code>
28 * <output>
29 * An ul-list of flash messages.
30 * </output>
31 *
32 * <code title="Output with custom css class">
33 * <f:flashMessages class="specialClass" />
34 * </code>
35 * <output>
36 * <ul class="specialClass">
37 * ...
38 * </ul>
39 * </output>
40 *
41 * <code title="TYPO3 core style">
42 * <f:flashMessages />
43 * </code>
44 * <output>
45 * <ul class="typo3-messages">
46 * <li class="alert alert-ok">
47 * <h4>Some Message Header</h4>
48 * Some message body
49 * </li>
50 * <li class="alert alert-notice">
51 * Some notice message without header
52 * </li>
53 * </ul>
54 * </output>
55 * <code title="Output flash messages as a description list">
56 * <f:flashMessages as="flashMessages">
57 * <dl class="messages">
58 * <f:for each="{flashMessages}" as="flashMessage">
59 * <dt>{flashMessage.code}</dt>
60 * <dd>{flashMessage.message}</dd>
61 * </f:for>
62 * </dl>
63 * </f:flashMessages>
64 * </code>
65 * <output>
66 * <dl class="messages">
67 * <dt>1013</dt>
68 * <dd>Some Warning Message.</dd>
69 * </dl>
70 * </output>
71 *
72 * <code title="Using a specific queue">
73 * <f:flashMessages queueIdentifier="myQueue" />
74 * </code>
75 *
76 * @api
77 */
78 class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper
79 {
80 /**
81 * @var string
82 */
83 protected $tagName = 'ul';
84
85 /**
86 * Initialize arguments
87 *
88 * @return void
89 * @api
90 */
91 public function initializeArguments()
92 {
93 $this->registerUniversalTagAttributes();
94 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
95 }
96
97 /**
98 * Renders FlashMessages and flushes the FlashMessage queue
99 * Note: This disables the current page cache in order to prevent FlashMessage output
100 * from being cached.
101 *
102 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
103 * @param string $as The name of the current flashMessage variable for rendering inside
104 * @return string rendered Flash Messages, if there are any.
105 * @api
106 */
107 public function render($as = null)
108 {
109 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
110 $flashMessages = $this->renderingContext->getControllerContext()->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
111 if ($flashMessages === null || count($flashMessages) === 0) {
112 return '';
113 }
114
115 if ($as === null) {
116 $content = $this->renderAsList($flashMessages);
117 } else {
118 $content = $this->renderFromTemplate($flashMessages, $as);
119 }
120
121 return $content;
122 }
123
124 /**
125 * Renders the flash messages as unordered list
126 *
127 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
128 * @return string
129 */
130 protected function renderAsList(array $flashMessages)
131 {
132 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
133 $tagContent = '';
134 $this->tag->addAttribute('class', $flashMessagesClass);
135 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
136 foreach ($flashMessages as $singleFlashMessage) {
137 $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
138 $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
139 if ($singleFlashMessage->getTitle() !== '') {
140 $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
141 }
142 $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
143 }
144 $this->tag->setContent($tagContent);
145 return $this->tag->render();
146 }
147
148 /**
149 * Renders the flash messages as nested divs
150 * Defer the rendering of Flash Messages to the template. In this case,
151 * the flash messages are stored in the template inside the variable specified
152 * in "as".
153 *
154 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
155 * @param string $as
156 * @return string
157 */
158 protected function renderFromTemplate(array $flashMessages, $as)
159 {
160 $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
161 $templateVariableContainer->add($as, $flashMessages);
162 $content = $this->renderChildren();
163 $templateVariableContainer->remove($as);
164
165 return $content;
166 }
167 }