‪TYPO3CMS  11.5
FlashMessageFinisher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 /*
19  * Inspired by and partially taken from the Neos.Form package (www.neos.io)
20  */
21 
23 
34 
55 {
59  protected ‪$defaultOptions = [
60  'messageBody' => null,
61  'messageTitle' => '',
62  'messageArguments' => [],
63  'messageCode' => null,
64  'severity' => ‪AbstractMessage::OK,
65  ];
66 
69 
71  {
72  $this->flashMessageService = ‪$flashMessageService;
73  }
74 
76  {
77  $this->extensionService = ‪$extensionService;
78  }
79 
86  protected function ‪executeInternal()
87  {
88  $messageBody = $this->‪parseOption('messageBody');
89  if (!is_string($messageBody)) {
90  throw new ‪FinisherException(sprintf('The message body must be of type string, "%s" given.', gettype($messageBody)), 1335980069);
91  }
92  $messageTitle = $this->‪parseOption('messageTitle');
93  $messageArguments = $this->‪parseOption('messageArguments');
94  $messageCode = $this->‪parseOption('messageCode');
95  $severity = $this->‪parseOption('severity');
96  switch ($severity) {
98  $message = GeneralUtility::makeInstance(Notice::class, $messageBody, $messageCode, $messageArguments, $messageTitle);
99  break;
101  $message = GeneralUtility::makeInstance(Warning::class, $messageBody, $messageCode, $messageArguments, $messageTitle);
102  break;
104  $message = GeneralUtility::makeInstance(Error::class, $messageBody, $messageCode, $messageArguments, $messageTitle);
105  break;
106  default:
107  $message = GeneralUtility::makeInstance(Message::class, $messageBody, $messageCode, $messageArguments, $messageTitle);
108  break;
109  }
110 
111  $flashMessage = GeneralUtility::makeInstance(
112  FlashMessage::class,
113  $message->render(),
114  $message->getTitle(),
115  $severity,
116  true
117  );
118 
119  // todo: this value has to be taken from the request directly in the future
120  $pluginNamespace = $this->extensionService->getPluginNamespace(
121  $this->finisherContext->getRequest()->getControllerExtensionName(),
122  $this->finisherContext->getRequest()->getPluginName()
123  );
124 
125  $this->flashMessageService->getMessageQueueByIdentifier('extbase.flashmessages.' . $pluginNamespace)->addMessage($flashMessage);
126  }
127 }
‪TYPO3\CMS\Core\Messaging\AbstractMessage
Definition: AbstractMessage.php:26
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher\$extensionService
‪ExtensionService $extensionService
Definition: FlashMessageFinisher.php:66
‪TYPO3\CMS\Extbase\Error\Notice
Definition: Notice.php:25
‪TYPO3\CMS\Form\Domain\Finishers
Definition: AbstractFinisher.php:22
‪TYPO3\CMS\Extbase\Error\Message
Definition: Message.php:24
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher
Definition: FlashMessageFinisher.php:55
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher\injectExtensionService
‪injectExtensionService(ExtensionService $extensionService)
Definition: FlashMessageFinisher.php:74
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher\executeInternal
‪executeInternal()
Definition: FlashMessageFinisher.php:85
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher
Definition: AbstractFinisher.php:42
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher\$flashMessageService
‪FlashMessageService $flashMessageService
Definition: FlashMessageFinisher.php:67
‪TYPO3\CMS\Form\Domain\Finishers\Exception\FinisherException
Definition: FinisherException.php:25
‪TYPO3\CMS\Extbase\Error\Warning
Definition: Warning.php:25
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:26
‪TYPO3\CMS\Extbase\Service\ExtensionService
Definition: ExtensionService.php:34
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher\$defaultOptions
‪array $defaultOptions
Definition: FlashMessageFinisher.php:58
‪TYPO3\CMS\Core\Messaging\AbstractMessage\NOTICE
‪const NOTICE
Definition: AbstractMessage.php:27
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher\parseOption
‪string array null parseOption(string $optionName)
Definition: AbstractFinisher.php:172
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Messaging\FlashMessageService
Definition: FlashMessageService.php:27
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Form\Domain\Finishers\FlashMessageFinisher\injectFlashMessageService
‪injectFlashMessageService(FlashMessageService $flashMessageService)
Definition: FlashMessageFinisher.php:69