0

In my Magento2 module I want to create admin system messages that are sticky and show up on every page in the adminhtml until the problem is solved. It's something important that the admin needs to address, otherwise the store will be severely impacted. But once the detected problem was solved, the message should go away.

As an example I'm looking at the messages that tell me when indexers or caches need to be refreshed. How can I create messages like that?

admin system message

I was looking at the core module AdminNotification which seems to be uniquely responsible for this. It looked at the involved templates and blocks to display these messages, and that all makes sense, but where are they created? I can't seem to figure out how to create these notifications.

There is a db table called admin_system_messages which stores something related to that, but I don't understand the content. The field identity just contains a long hex number. I took such a number and searched for it over all code and the entire database to find references, but nothing turned up. How does the module extract "One or more of the Cache Types are invalidated [...]" from this?

To be clear, I have seen the mechanism to add temporary messages which will be shown to the user ONCE, such as the confirmations when you delete or save something and it tells you that it was successful. This is NOT what I'm looking for, I want permanent messages on every page. I know it's obtrusive, but I'm convinced it's necessary in my case.

1
  • Hi you can write your login on header.phtml file which is shows on all admin pages magento/module-backend/view/adminhtml/templates/page/header.phtml
    – Vishal
    Commented Mar 17, 2016 at 11:21

1 Answer 1

2

I have solved this myself.

Your module needs a dependency to Magento_AdminNotification.

Then you create a model which implements MessageInterface, like this:

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
  }
}

That's really all there is to it. I spent so much time looking for the place in the code where these messages are triggered, because I didn't notice that they trigger themselves by using the isDisplayed() method.

You can get inspiration from other such messages used in core:

\Magento\AdminNotification\Model\System\Message\Baseurl \Magento\AdminNotification\Model\System\Message\CacheOutdated \Magento\AdminNotification\Model\System\Message\Security

1
  • I have tried code but not working in magento 2.2.6 Commented Dec 8, 2018 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.