如何创建自定义粘性管理系统消息?
-
29-09-2020 - |
题
在我的Magento2模块中,我想创建粘滞的管理系统消息,并显示在adminhtml的每个页面上,直到问题解决。这是管理员需要解决的重要问题,否则商店将受到严重影响。但是,一旦检测到的问题得到解决,消息应该消失。
作为一个例子,我正在查看告诉我何时需要刷新索引器或缓存的消息。我怎样才能创建这样的消息?
我在看核心模块 AdminNotification
这似乎是唯一的责任。它查看了所涉及的模板和块来显示这些消息,这一切都是有道理的,但它们是在哪里创建的?我似乎无法弄清楚如何创建这些通知。
有一个名为db表 admin_system_messages
它存储了与此相关的东西,但我不明白内容。领域 identity
只包含一个长的十六进制数。我拿了这样一个数字,并在所有代码和整个数据库中搜索它以查找引用,但没有任何结果。模块如何提取"一个或多个缓存类型无效[。..]"从这个?
要清楚的是,我已经看到了添加临时消息的机制,这些消息将向用户显示一次,例如当您删除或保存某些内容时的确认,它告诉您它是成功的。这不是我要找的,我想要每个页面上的永久消息。我知道这很突兀,但我相信这对我来说是必要的。
解决方案
我自己已经解决了这个问题。
您的模块需要依赖于Magento_AdminNotification。
然后创建一个实现MessageInterface的模型,如下所示:
class YourMessage implements \Magento\Framework\Notification\MessageInterface
{
public function getIdentity() {
...
}
public function isDisplayed() {
// write code to decide if this message should be shown or not
// return true to show it, false otherwise
}
public function getText() {
// output a static text or dynamically generate one
}
public function getSeverity() {
// if you return self::SEVERITY_MAJOR the sticky box on top will be empty
// if you return self::SEVERITY_CRITICAL the sticky box will always show your text
}
}
这就是它的全部。我花了很多时间在代码中寻找触发这些消息的位置,因为我没有注意到它们通过使用 isDisplayed()
方法。
您可以从core中使用的其他此类消息中获得灵感:
\Magento\AdminNotification\Model\System\Message\Baseurl
\Magento\AdminNotification\Model\System\Message\CacheOutdated
\Magento\AdminNotification\Model\System\Message\Security