TYPO3  7.6
FlashMessagesViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License, either version 3 *
9  * of the License, or (at your option) any later version. *
10  * *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
24 
87 {
88  const RENDER_MODE_UL = 'ul';
89  const RENDER_MODE_DIV = 'div';
90 
94  protected $tagName = 'ul';
95 
102  public function initializeArguments()
103  {
105  $this->registerArgument('queueIdentifier', 'string', 'Flash-message queue to use', false);
106  }
107 
119  public function render($renderMode = null, $as = null)
120  {
121  $queueIdentifier = isset($this->arguments['queueIdentifier']) ? $this->arguments['queueIdentifier'] : null;
122  $flashMessages = $this->controllerContext->getFlashMessageQueue($queueIdentifier)->getAllMessagesAndFlush();
123  if ($flashMessages === null || count($flashMessages) === 0) {
124  return '';
125  }
126  if ($renderMode !== null) {
127  GeneralUtility::deprecationLog('renderMode has been deprecated in TYPO3 CMS 7.3 and will be removed in TYPO3 CMS 8');
128  if ($renderMode === self::RENDER_MODE_DIV) {
129  $content = $this->renderDiv($flashMessages);
130  } else {
131  $content = $this->renderAsList($flashMessages);
132  }
133  } else {
134  if ($as === null) {
135  $content = $this->renderAsList($flashMessages);
136  } else {
137  $content = $this->renderFromTemplate($flashMessages, $as);
138  }
139  }
140 
141  return $content;
142  }
143 
150  protected function renderAsList(array $flashMessages)
151  {
152  $flashMessagesClass = $this->hasArgument('class') ? $this->arguments['class'] : 'typo3-messages';
153  $tagContent = '';
154  $this->tag->addAttribute('class', $flashMessagesClass);
156  foreach ($flashMessages as $singleFlashMessage) {
157  $severityClass = sprintf('alert %s', $singleFlashMessage->getClass());
158  $messageContent = htmlspecialchars($singleFlashMessage->getMessage());
159  if ($singleFlashMessage->getTitle() !== '') {
160  $messageContent = sprintf('<h4>%s</h4>', htmlspecialchars($singleFlashMessage->getTitle())) . $messageContent;
161  }
162  $tagContent .= sprintf('<li class="%s">%s</li>', htmlspecialchars($severityClass), $messageContent);
163  }
164  $this->tag->setContent($tagContent);
165  return $this->tag->render();
166  }
167 
178  protected function renderFromTemplate(array $flashMessages, $as)
179  {
180  $templateVariableContainer = $this->renderingContext->getTemplateVariableContainer();
181  $templateVariableContainer->add($as, $flashMessages);
182  $content = $this->renderChildren();
183  $templateVariableContainer->remove($as);
184 
185  return $content;
186  }
187 
195  protected function renderDiv(array $flashMessages)
196  {
197  $this->tag->setTagName('div');
198  if ($this->hasArgument('class')) {
199  $this->tag->addAttribute('class', $this->arguments['class']);
200  } else {
201  $this->tag->addAttribute('class', 'typo3-messages');
202  }
203  $tagContent = '';
204  foreach ($flashMessages as $singleFlashMessage) {
205  $tagContent .= $singleFlashMessage->render();
206  }
207  $this->tag->setContent($tagContent);
208  return $this->tag->render();
209  }
210 }