问题
Basically: I want to display a flash message in the BE whenever the user enters a value that is not a valid IPv4. I want to do this in a hook, but just using the code from the Typo3 documentation for flash messages doesn't work there seems to be a major missunderstanding on my side maybe. Here is my code so far:
<?php
namespace Cjk\Icingaconfgen\Hook;
class EvalHook {
function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, &$pObj) {
if (!filter_var($fieldArray[IPv4], FILTER_VALIDATE_IP)){
//here i want to put the flash message code
}
}
}
?>
Using the code from the documentation here:
$message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class,
'My message text',
'Message Header', // [optional] the header
\TYPO3\CMS\Core\Messaging\FlashMessage::WARNING, // [optional] the severity defaults to \TYPO3\CMS\Core\Messaging\FlashMessage::OK
true // [optional] whether the message should be stored in the session or only in the \TYPO3\CMS\Core\Messaging\FlashMessageQueue object (default is false)
);
with the addition of the line:
$message->render();
doesn't work. What am i missing?
回答1:
Have you tried adding the FlashMessage to the message queue?
use TYPO3\CMS\Core\Messaging\FlashMessageService;
// ... omitted some use statements
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
//...
$flashMessageService = $this->objectManager->get(lashMessageService::class);
$messageQueue = $flashMessageService->getMessageQueueByIdentifier();
$messageQueue->addMessage($message);
You wouldn't need that part, if you used the flash messages in the Controller.
https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/FlashMessages/Index.html
来源:https://stackoverflow.com/questions/47590844/displaying-a-flash-message-in-the-backend-using-a-hook-doesnt-work-somehow