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