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.