[TASK] Streamline phpdoc annotations in EXT:fluid
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / FlashMessagesViewHelper.php
index 42a0b0d..bb106a9 100644 (file)
@@ -1,25 +1,25 @@
 <?php
 namespace TYPO3\CMS\Fluid\ViewHelpers;
 
-/*                                                                        *
- * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
- *                                                                        *
- * It is free software; you can redistribute it and/or modify it under    *
- * the terms of the GNU Lesser General Public License, either version 3   *
- *  of the License, or (at your option) any later version.                *
- *                                                                        *
- *                                                                        *
- * This script is distributed in the hope that it will be useful, but     *
- * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
- * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
- * General Public License for more details.                               *
- *                                                                        *
- * You should have received a copy of the GNU Lesser General Public       *
- * License along with the script.                                         *
- * If not, see http://www.gnu.org/licenses/lgpl.html                      *
- *                                                                        *
- * The TYPO3 project - inspiring people to share!                         *
- *                                                                        */
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Messaging\FlashMessageRendererResolver;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
+use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
+
 /**
  * View helper which renders the flash messages (if there are any) as an unsorted list.
  *
@@ -32,134 +32,102 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
  * <f:flashMessages />
  * </code>
  * <output>
- * An ul-list of flash messages.
- * </output>
- *
- * <code title="Output with custom css class">
- * <f:flashMessages class="specialClass" />
- * </code>
- * <output>
- * <ul class="specialClass">
- * ...
- * </ul>
+ * A list of flash messages.
  * </output>
  *
  * <code title="TYPO3 core style">
- * <f:flashMessages renderMode="div" />
+ * <f:flashMessages />
  * </code>
  * <output>
  * <div class="typo3-messages">
- * <div class="typo3-message message-ok">
- * <div class="message-header">Some Message Header</div>
- * <div class="message-body">Some message body</div>
- * </div>
- * <div class="typo3-message message-notice">
- * <div class="message-body">Some notice message without header</div>
- * </div>
+ *  <div class="alert alert-info">
+ *      <div class="media">
+ *          <div class="media-left">
+ *              <span class="fa-stack fa-lg">
+ *                  <i class="fa fa-circle fa-stack-2x"></i>
+ *                  <i class="fa fa-info fa-stack-1x"></i>
+ *              </span>
+ *          </div>
+ *          <div class="media-body">
+ *              <h4 class="alert-title">Info - Title for Info message</h4>
+ *              <p class="alert-message">Message text here.</p>
+ *          </div>
+ *      </div>
+ *  </div>
  * </div>
  * </output>
+ * <code title="Output flash messages as a description list">
+ * <f:flashMessages as="flashMessages">
+ *     <dl class="messages">
+ *     <f:for each="{flashMessages}" as="flashMessage">
+ *             <dt>{flashMessage.code}</dt>
+ *             <dd>{flashMessage.message}</dd>
+ *     </f:for>
+ *     </dl>
+ * </f:flashMessages>
+ * </code>
+ * <output>
+ * <dl class="messages">
+ *     <dt>1013</dt>
+ *     <dd>Some Warning Message.</dd>
+ * </dl>
+ * </output>
  *
- * @api
+ * <code title="Using a specific queue">
+ * <f:flashMessages queueIdentifier="myQueue" />
+ * </code>
  */
-class FlashMessagesViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
-
-       const RENDER_MODE_UL = 'ul';
-       const RENDER_MODE_DIV = 'div';
-
-       /**
-        * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
-        */
-       protected $contentObject;
-
-       /**
-        * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
-        */
-       protected $configurationManager;
+class FlashMessagesViewHelper extends AbstractViewHelper
+{
+    use CompileWithRenderStatic;
 
-       /**
-        * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
-        * @return void
-        */
-       public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
-               $this->configurationManager = $configurationManager;
-               $this->contentObject = $this->configurationManager->getContentObject();
-       }
+    /**
+     * ViewHelper outputs HTML therefore output escaping has to be disabled
+     *
+     * @var bool
+     */
+    protected $escapeOutput = false;
 
-       /**
-        * Initialize arguments
-        *
-        * @return void
-        * @api
-        */
-       public function initializeArguments() {
-               $this->registerUniversalTagAttributes();
-       }
+    /**
+     * Initialize arguments
+     */
+    public function initializeArguments()
+    {
+        $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use');
+        $this->registerArgument('as', 'string', 'The name of the current flashMessage variable for rendering inside');
+    }
 
-       /**
-        * Renders FlashMessages and flushes the FlashMessage queue
-        * Note: This disables the current page cache in order to prevent FlashMessage output
-        * from being cached.
-        *
-        * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
-        * @param string $renderMode one of the RENDER_MODE_* constants
-        * @return string rendered Flash Messages, if there are any.
-        * @api
-        */
-       public function render($renderMode = self::RENDER_MODE_UL) {
-               $flashMessages = $this->controllerContext->getFlashMessageQueue()->getAllMessagesAndFlush();
-               if ($flashMessages === NULL || count($flashMessages) === 0) {
-                       return '';
-               }
-               if (isset($GLOBALS['TSFE']) && $this->contentObject->getUserObjectType() === \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::OBJECTTYPE_USER) {
-                       $GLOBALS['TSFE']->no_cache = 1;
-               }
-               switch ($renderMode) {
-                       case self::RENDER_MODE_UL:
-                               return $this->renderUl($flashMessages);
-                       case self::RENDER_MODE_DIV:
-                               return $this->renderDiv($flashMessages);
-                       default:
-                               throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Invalid render mode "' . $renderMode . '" passed to FlashMessageViewhelper', 1290697924);
-               }
-       }
+    /**
+     * Renders FlashMessages and flushes the FlashMessage queue
+     * Note: This disables the current page cache in order to prevent FlashMessage output
+     * from being cached.
+     *
+     * @see \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::no_cache
+     * @param array $arguments
+     * @param \Closure $renderChildrenClosure
+     * @param RenderingContextInterface $renderingContext
+     * @return mixed
+     */
+    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
+    {
+        $as = $arguments['as'];
+        $queueIdentifier = $arguments['queueIdentifier'] ?? null;
+        $flashMessages = $renderingContext->getControllerContext()
+            ->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
+        if ($flashMessages === null || count($flashMessages) === 0) {
+            return '';
+        }
 
-       /**
-        * Renders the flash messages as unordered list
-        *
-        * @param array $flashMessages array<\TYPO3\CMS\Core\Messaging\FlashMessage>
-        * @return string
-        */
-       protected function renderUl(array $flashMessages) {
-               $this->tag->setTagName('ul');
-               if ($this->hasArgument('class')) {
-                       $this->tag->addAttribute('class', $this->arguments['class']);
-               }
-               $tagContent = '';
-               foreach ($flashMessages as $singleFlashMessage) {
-                       $tagContent .= '<li>' . htmlspecialchars($singleFlashMessage->getMessage()) . '</li>';
-               }
-               $this->tag->setContent($tagContent);
-               return $this->tag->render();
-       }
+        if ($as === null) {
+            return GeneralUtility::makeInstance(FlashMessageRendererResolver::class)
+                ->resolve()
+                ->render($flashMessages);
+        }
+        $templateVariableContainer = $renderingContext->getVariableProvider();
+        $templateVariableContainer->add($as, $flashMessages);
+        $content = $renderChildrenClosure();
+        $templateVariableContainer->remove($as);
 
-       /**
-        * Renders the flash messages as nested divs
-        *
-        * @param array $flashMessages array<\TYPO3\CMS\Core\Messaging\FlashMessage>
-        * @return string
-        */
-       protected function renderDiv(array $flashMessages) {
-               $this->tag->setTagName('div');
-               if ($this->hasArgument('class')) {
-                       $this->tag->addAttribute('class', $this->arguments['class']);
-               } else {
-                       $this->tag->addAttribute('class', 'typo3-messages');
-               }
-               $tagContent = '';
-               foreach ($flashMessages as $singleFlashMessage) {
-                       $tagContent .= $singleFlashMessage->render();
-               }
-               $this->tag->setContent($tagContent);
-               return $this->tag->render();
-       }
+        return $content;
+    }
 }