[TASK] Remove usages of getTemplateVariableContainer
[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 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
17
18 /**
19 * View helper which renders the flash messages (if there are any) as an unsorted list.
20 *
21 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
22 *
23 *
24 * = Examples =
25 *
26 * <code title="Simple">
27 * <f:flashMessages />
28 * </code>
29 * <output>
30 * An ul-list of flash messages.
31 * </output>
32 *
33 * <code title="Output with custom css class">
34 * <f:flashMessages class="specialClass" />
35 * </code>
36 * <output>
37 * <div class="specialClass">
38 * ...
39 * </ul>
40 * </output>
41 *
42 * <code title="TYPO3 core style">
43 * <f:flashMessages />
44 * </code>
45 * <output>
46 * <div class="typo3-messages">
47 * <div class="alert alert-info">
48 * <div class="media">
49 * <div class="media-left">
50 * <span class="fa-stack fa-lg">
51 * <i class="fa fa-circle fa-stack-2x"></i>
52 * <i class="fa fa-info fa-stack-1x"></i>
53 * </span>
54 * </div>
55 * <div class="media-body">
56 * <h4 class="alert-title">Info - Title for Info message</h4>
57 * <p class="alert-message">Message text here.</p>
58 * </div>
59 * </div>
60 * </div>
61 * </div>
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 AbstractTagBasedViewHelper
87 {
88 /**
89 * @var string
90 */
91 protected $tagName = 'div';
92
93 /**
94 * Initialize arguments
95 *
96 * @return void
97 * @api
98 */
99 public function initializeArguments()
100 {
101 parent::initializeArguments();
102 $this->registerUniversalTagAttributes();
103 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use');
104 $this->registerArgument('as', 'string', 'The name of the current flashMessage variable for rendering inside');
105 }
106
107 /**
108 * Renders FlashMessages and flushes the FlashMessage queue
109 * Note: This disables the current page cache in order to prevent FlashMessage output
110 * from being cached.
111 *
112 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
113 * @return string rendered Flash Messages, if there are any.
114 * @api
115 */
116 public function render()
117 {
118 $as = $this->arguments['as'];
119 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
120 $flashMessages = $this->renderingContext->getControllerContext()->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
121 if ($flashMessages === null || count($flashMessages) === 0) {
122 return '';
123 }
124
125 if ($as === null) {
126 $content = $this->renderDefault($flashMessages);
127 } else {
128 $content = $this->renderFromTemplate($flashMessages, $as);
129 }
130
131 return $content;
132 }
133
134 /**
135 * Renders the flash messages as unordered list
136 *
137 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
138 * @return string
139 */
140 protected function renderDefault(array $flashMessages)
141 {
142 $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
143 $tagContent = '';
144 $this->tag->addAttribute('class', $flashMessagesClass);
145 /** @var $singleFlashMessage \TYPO3\CMS\Core\Messaging\FlashMessage */
146 foreach ($flashMessages as $singleFlashMessage) {
147 $tagContent .= $singleFlashMessage->getMessageAsMarkup();
148 }
149 $this->tag->setContent($tagContent);
150 return $this->tag->render();
151 }
152
153 /**
154 * Renders the flash messages as nested divs
155 * Defer the rendering of Flash Messages to the template. In this case,
156 * the flash messages are stored in the template inside the variable specified
157 * in "as".
158 *
159 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
160 * @param string $as
161 * @return string
162 */
163 protected function renderFromTemplate(array $flashMessages, $as)
164 {
165 $templateVariableContainer = $this->renderingContext->getVariableProvider();
166 $templateVariableContainer->add($as, $flashMessages);
167 $content = $this->renderChildren();
168 $templateVariableContainer->remove($as);
169
170 return $content;
171 }
172 }