使用钩子在后端显示 Flash 消息以某种方式不起作用 [英] Displaying a flash message in the Backend using a hook doesn't work somehow

查看:18
本文介绍了使用钩子在后端显示 Flash 消息以某种方式不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上:每当用户输入一个无效的 IPv4 值时,我都希望在 BE 中显示一条闪现消息.我想在一个钩子中做到这一点,但仅使用 Typo3 文档中的代码来处理 flash 消息是行不通的,我这边似乎存在重大误解.到目前为止,这是我的代码:

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)
);

加上一行:

$message->render();

不起作用.我错过了什么?

doesn't work. What am i missing?

推荐答案

您是否尝试过将 FlashMessage 添加到消息队列中?

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);

如果您使用 Controller 中的 flash 消息,则不需要该部分.

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

这篇关于使用钩子在后端显示 Flash 消息以某种方式不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆