[BUGFIX] Ensure arguments for instances of AbstractTagBasedViewHelper
[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 parent::initializeArguments();
94 $this->registerUniversalTagAttributes();
95 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
96 }
97
98 /**
99 * Renders FlashMessages and flushes the FlashMessage queue
100 * Note: This disables the current page cache in order to prevent FlashMessage output
101 * from being cached.
102 *
103 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
104 * @param string $as The name of the current flashMessage variable for rendering inside
105 * @return string rendered Flash Messages, if there are any.
106 * @api
107 */
108 public function render($as = null)
109 {
110 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
111 $flashMessages = $this->renderingContext->getControllerContext()->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
112 if ($flashMessages === null || count($flashMessages) === 0) {
113 return '';
114 }
115
116 if ($as === null) {
117 $content = $this->renderAsList($flashMessages);
118 } else {
119 $content = $this->renderFromTemplate($flashMessages, $as);
120 }
121
122 return $content;
123 }
124
125 /**
126 * Renders the flash messages as unordered list
127 *
128 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
129 * @return string
130 */
131 protected function renderAsList(array $flashMessages)
132 {
133 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
134 $tagContent = '';
135 $this->tag->addAttribute('class', $flashMessagesClass);
136 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
137 foreach ($flashMessages as $singleFlashMessage) {
138 $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
139 $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
140 if ($singleFlashMessage->getTitle() !== '') {
141 $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
142 }
143 $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
144 }
145 $this->tag->setContent($tagContent);
146 return $this->tag->render();
147 }
148
149 /**
150 * Renders the flash messages as nested divs
151 * Defer the rendering of Flash Messages to the template. In this case,
152 * the flash messages are stored in the template inside the variable specified
153 * in "as".
154 *
155 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
156 * @param string $as
157 * @return string
158 */
159 protected function renderFromTemplate(array $flashMessages, $as)
160 {
161 $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
162 $templateVariableContainer->add($as, $flashMessages);
163 $content = $this->renderChildren();
164 $templateVariableContainer->remove($as);
165
166 return $content;
167 }
168 }