[!!!][FEATURE] Refactoring of FlashMessage rendering
[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 use TYPO3\CMS\Core\Messaging\FlashMessageRendererResolver;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
20
21 /**
22 * View helper which renders the flash messages (if there are any) as an unsorted list.
23 *
24 * In case you need custom Flash Message HTML output, please write your own ViewHelper for the moment.
25 *
26 *
27 * = Examples =
28 *
29 * <code title="Simple">
30 * <f:flashMessages />
31 * </code>
32 * <output>
33 * A list of flash messages.
34 * </output>
35 *
36 * <code title="TYPO3 core style">
37 * <f:flashMessages />
38 * </code>
39 * <output>
40 * <div class="typo3-messages">
41 * <div class="alert alert-info">
42 * <div class="media">
43 * <div class="media-left">
44 * <span class="fa-stack fa-lg">
45 * <i class="fa fa-circle fa-stack-2x"></i>
46 * <i class="fa fa-info fa-stack-1x"></i>
47 * </span>
48 * </div>
49 * <div class="media-body">
50 * <h4 class="alert-title">Info - Title for Info message</h4>
51 * <p class="alert-message">Message text here.</p>
52 * </div>
53 * </div>
54 * </div>
55 * </div>
56 * </output>
57 * <code title="Output flash messages as a description list">
58 * <f:flashMessages as="flashMessages">
59 * <dl class="messages">
60 * <f:for each="{flashMessages}" as="flashMessage">
61 * <dt>{flashMessage.code}</dt>
62 * <dd>{flashMessage.message}</dd>
63 * </f:for>
64 * </dl>
65 * </f:flashMessages>
66 * </code>
67 * <output>
68 * <dl class="messages">
69 * <dt>1013</dt>
70 * <dd>Some Warning Message.</dd>
71 * </dl>
72 * </output>
73 *
74 * <code title="Using a specific queue">
75 * <f:flashMessages queueIdentifier="myQueue" />
76 * </code>
77 *
78 * @api
79 */
80 class FlashMessagesViewHelper extends AbstractViewHelper
81 {
82 /**
83 * ViewHelper outputs HTML therefore output escaping has to be disabled
84 *
85 * @var bool
86 */
87 protected $escapeOutput = false;
88
89 /**
90 * Initialize arguments
91 *
92 * @return void
93 * @api
94 */
95 public function initializeArguments()
96 {
97 parent::initializeArguments();
98 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use');
99 $this->registerArgument('as', 'string', 'The name of the current flashMessage variable for rendering inside');
100 }
101
102 /**
103 * Renders FlashMessages and flushes the FlashMessage queue
104 * Note: This disables the current page cache in order to prevent FlashMessage output
105 * from being cached.
106 *
107 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
108 * @return string rendered Flash Messages, if there are any.
109 * @api
110 */
111 public function render()
112 {
113 $as = $this->arguments['as'];
114 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
115 $flashMessages = $this->renderingContext->getControllerContext()
116 ->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
117 if ($flashMessages === null || count($flashMessages) === 0) {
118 return '';
119 }
120
121 if ($as === null) {
122 $content = $this->renderDefault($flashMessages);
123 } else {
124 $content = $this->renderFromTemplate($flashMessages, $as);
125 }
126
127 return $content;
128 }
129
130 /**
131 * Renders the flash messages
132 *
133 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
134 * @return string
135 */
136 protected function renderDefault(array $flashMessages) : string
137 {
138 return GeneralUtility::makeInstance(FlashMessageRendererResolver::class)
139 ->resolve()
140 ->render($flashMessages);
141 }
142
143 /**
144 * Renders the flash messages as nested divs
145 * Defer the rendering of Flash Messages to the template. In this case,
146 * the flash messages are stored in the template inside the variable specified
147 * in "as".
148 *
149 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
150 * @param string $as
151 * @return string
152 */
153 protected function renderFromTemplate(array $flashMessages, $as) : string
154 {
155 $templateVariableContainer = $this->renderingContext->getVariableProvider();
156 $templateVariableContainer->add($as, $flashMessages);
157 $content = $this->renderChildren();
158 $templateVariableContainer->remove($as);
159
160 return $content;
161 }
162 }