[+FEATURE] Fluid (ViewHelpers): Add render mode to FlashMessage ViewHelper
authorBastian Waidelich <bastian@typo3.org>
Sat, 27 Nov 2010 11:36:24 +0000 (11:36 +0000)
committerBastian Waidelich <bastian@typo3.org>
Sat, 27 Nov 2010 11:36:24 +0000 (11:36 +0000)
Since r3062 Extbase uses the t3lib_FlashMessageQueue to store flashmessages.
Now you can output them including title and severity, making it possible
to render flashmessages just like the ones from the core in BE mode.
To keep backwards compatibility, the default rendering still outputs
only the message bodies in an unordered list. Set the renderMode argument
to "div" in order to display the new style:
<f:flashMessages renderMode="div" />

Relates to: #10821

typo3/sysext/fluid/Classes/ViewHelpers/FlashMessagesViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/RenderFlashMessagesViewHelper.php

index c0940eb..7714a67 100644 (file)
  */
 class Tx_Fluid_ViewHelpers_FlashMessagesViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
 
-       /**
-        * @var string
-        */
-       protected $tagName = 'ul';
+       const RENDER_MODE_UL = 'ul';
+       const RENDER_MODE_DIV = 'div';
 
        /**
         * Initialize arguments
@@ -66,21 +64,64 @@ class Tx_Fluid_ViewHelpers_FlashMessagesViewHelper extends Tx_Fluid_Core_ViewHel
        /**
         * Render method.
         *
+        * @param string $renderMode one of the RENDER_MODE_* constants
         * @return string rendered Flash Messages, if there are any.
         * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
         * @api
         */
-       public function render() {
-               $flashMessages = $this->controllerContext->getFlashMessageContainer()->getAllAndFlush();
-               if (count($flashMessages) > 0) {
-                       $tagContent = '';
-                       foreach ($flashMessages as $singleFlashMessage) {
-                               $tagContent .=  '<li>' . htmlspecialchars($singleFlashMessage) . '</li>';
-                       }
-                       $this->tag->setContent($tagContent);
-                       return $this->tag->render();
+       public function render($renderMode = self::RENDER_MODE_UL) {
+               $flashMessages = $this->controllerContext->getFlashMessageContainer()->getAllMessagesAndFlush();
+               if ($flashMessages === NULL || count($flashMessages) === 0) {
+                       return '';
+               }
+               switch ($renderMode) {
+                       case self::RENDER_MODE_UL:
+                               return $this->renderUl($flashMessages);
+                       case self::RENDER_MODE_DIV:
+                               return $this->renderDiv($flashMessages);
+                       default:
+                               throw new Tx_Fluid_Core_ViewHelper_Exception('Invalid render mode "' . $renderMode . '" passed to FlashMessageViewhelper', 1290697924);
+               }
+       }
+
+       /**
+        * Renders the flash messages as unordered list
+        *
+        * @param array $flashMessages array<t3lib_FlashMessage>
+        * @return string
+        */
+       protected function renderUl(array $flashMessages) {
+               $this->tag->setTagName('ul');
+               if ($this->arguments->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();
+       }
+
+       /*
+        * Renders the flash messages as nested divs
+        *
+        * @param array $flashMessages array<t3lib_FlashMessage>
+        * @return string
+        */
+       protected function renderDiv(array $flashMessages) {
+               $this->tag->setTagName('div');
+               if ($this->arguments->hasArgument('class')) {
+                       $this->tag->addAttribute('class', $this->arguments['class']);
+               } else {
+                       $this->tag->addAttribute('class', 'typo3-messages');
+               }
+               $tagContent = '';
+               foreach ($flashMessages as $singleFlashMessage) {
+                       $tagContent .= $singleFlashMessage->render();
                }
-               return '';
+               $this->tag->setContent($tagContent);
+               return $this->tag->render();
        }
 }
 
index a08a2e0..ba8d393 100644 (file)
  * Deprecated. Use <f:flashMessages> instead!
  *
  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
+ * @deprecated since Extbase 1.3.0; will be removed in Extbase 1.5.0
  */
 class Tx_Fluid_ViewHelpers_RenderFlashMessagesViewHelper extends Tx_Fluid_ViewHelpers_FlashMessagesViewHelper {
 
+       /**
+        * @return void
+        */
+       public function initialize() {
+               t3lib_div::logDeprecatedFunction();
+               return parent::initialize();
+       }
 }
 
 ?>