[TASK] Streamline phpdoc annotations in EXT:fluid
[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 class FlashMessagesViewHelper extends AbstractViewHelper
81 {
82 use CompileWithRenderStatic;
83
84 /**
85 * ViewHelper outputs HTML therefore output escaping has to be disabled
86 *
87 * @var bool
88 */
89 protected $escapeOutput = false;
90
91 /**
92 * Initialize arguments
93 */
94 public function initializeArguments()
95 {
96 $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use');
97 $this->registerArgument('as', 'string', 'The name of the current flashMessage variable for rendering inside');
98 }
99
100 /**
101 * Renders FlashMessages and flushes the FlashMessage queue
102 * Note: This disables the current page cache in order to prevent FlashMessage output
103 * from being cached.
104 *
105 * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
106 * @param array $arguments
107 * @param \Closure $renderChildrenClosure
108 * @param RenderingContextInterface $renderingContext
109 * @return mixed
110 */
111 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
112 {
113 $as = $arguments['as'];
114 $queueIdentifier = $arguments['queueIdentifier'] ?? null;
115 $flashMessages = $renderingContext->getControllerContext()
116 ->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
117 if ($flashMessages === null || count($flashMessages) === 0) {
118 return '';
119 }
120
121 if ($as === null) {
122 return GeneralUtility::makeInstance(FlashMessageRendererResolver::class)
123 ->resolve()
124 ->render($flashMessages);
125 }
126 $templateVariableContainer = $renderingContext->getVariableProvider();
127 $templateVariableContainer->add($as, $flashMessages);
128 $content = $renderChildrenClosure();
129 $templateVariableContainer->remove($as);
130
131 return $content;
132 }
133 }