0

I need to create custom sticky admin system messages.

Here is my code, But not working

class StickyMessages implements \Magento\Framework\Notification\MessageInterface
{
  public function getIdentity() {
   echo 'testing';
  }

  public function isDisplayed() {
    // write code to decide if this message should be shown or not
    // return true to show it, false otherwise

    echo 'testing';
  }

  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
    return self::SEVERITY_CRITICAL;
  }
}

enter image description here

I am getting admin to distort on loading

enter image description here

enter image description here

I want to show an open message on top. So that when we login then simply show the mesaage

enter image description here

2
  • can you update your di.xml and Message file in question ? Commented Nov 29, 2019 at 9:38
  • The code is working but needs to show my own message. no need to open the dropdown. Please check the screenshot.
    – Shomita
    Commented Nov 29, 2019 at 10:54

2 Answers 2

0

You didn't write text in getText() function. Update your code like below way :

class StickyMessages implements \Magento\Framework\Notification\MessageInterface
{
  public function getIdentity() {
   return 'testing';
  }

  public function isDisplayed() {
    // write code to decide if this message should be shown or not
    // return true to show it, false otherwise
    return true;

  }

  public function getText() {
    // output a static text or dynamically generate one
    return 'testing';
  }

  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
    return self::SEVERITY_CRITICAL;
  }
}

EDIT :

You need to call model in di.xml file :

app/code/Vendor/Module/etc/adminhtml/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Notification\MessageList">
        <arguments>
            <argument name="messages" xsi:type="array">
                <item name="indexer_invalid_message" xsi:type="string">Vendor\Module\Model\StickyMessages</item>
            </argument>
        </arguments>
    </type>
</config>

Reference : vendor/magento/module-indexer/etc/adminhtml/di.xml

10
  • Need to call the model?. I haven't call it
    – Shomita
    Commented Nov 29, 2019 at 7:17
  • Not working. Can you please check
    – Shomita
    Commented Nov 29, 2019 at 7:21
  • Yes. You need to call model in di.xml file. Commented Nov 29, 2019 at 7:28
  • Please screenshot images in comment
    – Shomita
    Commented Nov 29, 2019 at 8:49
  • Can you please check?
    – Shomita
    Commented Nov 29, 2019 at 8:57
0

Firstly, you need to create “di.xml” file at App\code\Vendor\Extension\etc\adminhtml with the below code.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\Framework\Notification\MessageList">
        <arguments>
            <argument name="messages" xsi:type="array">
                <item name="AdminQuoteMessages" xsi:type="string">Vendor\Extension\Model\Admin\Quote\Messages</item>
            </argument>
        </arguments>
    </type>
</config>

Once the file is created, you need to create another file at App\code\Vendor\Extension\Model\Admin\Quote\ with the below code and name it as “Messages.php”.

<?php

namespace Vendor\Extension\Model\Admin\Quote;

use Magento\Security\Model\ResourceModel\AdminSessionInfo\Collection;
use Magento\Backend\Model\UrlInterface;
use Magento\Backend\Model\Auth\Session;
use Magento\Framework\Notification\MessageInterface;

class Messages implements MessageInterface
{

    protected $backendUrl;
    private $adminSessionInfoCollection;
    protected $authSession;

    public function __construct(
        Collection $adminSessionInfoCollection,
        UrlInterface $backendUrl,
                                Session $authSession
    ) {
        $this->authSession = $authSession;
        $this->backendUrl = $backendUrl;
        $this->adminSessionInfoCollection = $adminSessionInfoCollection;
    }

    public function getText()
    {
        $message = __('Admin notification Add Successfully.');
        return $message;
    }
    public function getIdentity()
    {
        return md5('VENDOR_EXTENSION' . $this->authSession->getUser()->getLogdate());
    }
    public function isDisplayed()
    {
        return true;
    }
                public function getSeverity()
                {
                // From here you can change notification message type.
        return \Magento\Framework\Notification\MessageInterface::SEVERITY_CRITICAL;
    }
}

If you more refer link and How to Add Multiple Admin Notification Messages in Magento 2

Thank You...

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.