Magento-logo

Adding Magento Admin Notifications To Your Extension

Magento administrator interface has a nice notification system placed right under the main navigation. By default it is used by Magento core modules but it is quite easy to broadcast your own messages there.

All you have to do is to add your own block under `notifications` handler of `adminhtml` layout. Assuming that you already have a Magento extension first of all go to your ect/config.xml file and the following node:

<?xml version="1.0"?>
<config>
    ...
    <adminhtml>
        ...
        <layout>
            <updates>
                <index>
                    <file>your-group-name/your-extension-name.xml</file>
                </index>
            </updates>
        </layout>
        ...
    </adminhtml>
    ...
</config>

This will tell Magento to include your layout update XML while rendering admin pages. Now create a actual file under app/design/adminhtml/default/default/layout/your-group-name/your-extension-name.xml with the following content:

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="notifications">
            <block type="your-extension-name/adminhtml_notifications" name="your-extension-name_notifications" template="your-extension-group/your-extension-name/notifications.phtml"/>
        </reference>
    </default>
</layout>

This code will try to create a block of specified type and append it to `notifications` block. Let’s create a class for your block first. The path should be app/code/community/YourExtensionGroup/YourExtensionName/Block/Adminhtml/Notifications.php

<?php

class YourExtensionGroup_YourExtensionName_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
{
    public function getMessage()
    {
        /*
          * Here you have check if there's a message to be displayed or not
          */
        $message = 'Hello World!';
        return $message;
    }
}

We have just added a single method to our class which will determine if there are messages to be displayed or not. The last step is to add a template file under app/design/adminhtml/default/default/template/your-extension-group/your-extension-name/notifications.phtml

<?php if ($message = $this->getMessage()) : ?>
    <div class="notification-global"><?php echo $message ?></div>
<?php endif; ?>

That’s all. Put it into good use.

5 Kommentare zu «Adding Magento Admin Notifications To Your Extension»

  1. Pingback: How to add custom notification in Magento - BlogoSfera

  2. Hi Tim,

    I have done this with your mentioned code, it is actually really good, can i know how to call this notification message when a function calls only?, kindly give instructions for that.

    Thankyou

  3. I’ve done and it doen’t work.

    It seems when Tim copied code from Mage/Index he forget to replace one thing:
    In your module etc/config.xml add code:

    your-group-name/your-extension-name.xml

    It’s 1st suggestion of the article.

    So, If you fix it – It will WORK FINE! ))
    Thanx!

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert