[TASK] Streamline return tags in phpdocs
[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 * @api
93 */
94 public function initializeArguments()
95 {
96 parent::initializeArguments();
97 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use');
98 $this->registerArgument('as', 'string', 'The name of the current flashMessage variable for rendering inside');
99 }
100
101 /**
102 * Renders FlashMessages and flushes the FlashMessage queue
103 * Note: This disables the current page cache in order to prevent FlashMessage output
104 * from being cached.
105 *
106 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
107 * @return string rendered Flash Messages, if there are any.
108 * @api
109 */
110 public function render()
111 {
112 $as = $this->arguments['as'];
113 $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
114 $flashMessages = $this->renderingContext->getControllerContext()
115 ->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
116 if ($flashMessages === null || count($flashMessages) === 0) {
117 return '';
118 }
119
120 if ($as === null) {
121 $content = $this->renderDefault($flashMessages);
122 } else {
123 $content = $this->renderFromTemplate($flashMessages, $as);
124 }
125
126 return $content;
127 }
128
129 /**
130 * Renders the flash messages
131 *
132 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
133 * @return string
134 */
135 protected function renderDefault(array $flashMessages) : string
136 {
137 return GeneralUtility::makeInstance(FlashMessageRendererResolver::class)
138 ->resolve()
139 ->render($flashMessages);
140 }
141
142 /**
143 * Renders the flash messages as nested divs
144 * Defer the rendering of Flash Messages to the template. In this case,
145 * the flash messages are stored in the template inside the variable specified
146 * in "as".
147 *
148 * @param array $flashMessages \TYPO3\CMS\Core\Messaging\FlashMessage[]
149 * @param string $as
150 * @return string
151 */
152 protected function renderFromTemplate(array $flashMessages, $as) : string
153 {
154 $templateVariableContainer = $this->renderingContext->getVariableProvider();
155 $templateVariableContainer->add($as, $flashMessages);
156 $content = $this->renderChildren();
157 $templateVariableContainer->remove($as);
158
159 return $content;
160 }
161 }