Displaying a flash message in the Backend using a hook doesn't work somehow

走远了吗. 提交于 2020-01-06 02:01:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!