Codementor Events

Using Global Notification Area in Your Magento Extension

Published Feb 17, 2016Last updated Jan 16, 2017
Using Global Notification Area in Your Magento Extension

The image shows my extension outputting a message that needs to be configured.


This tutorial covers how you can extend your Magento extension to output content to the global notification area that Magento admin uses internally. This is not enough to make a complete extension. You can seek that information elsewhere, but a good start is Silk Module Creator.

You will need a few things to hook into the notifications area.

You'll also need to reference the notifications area with a block in your extension's layout file.

File structure

These are the files we are going to cover below:

app/code/local/Yourcompany/Youextension/Block/Adminhtml/Notifications.php     
    app/code/local/Yourcompany/Youextension/etc/config.xml
    app/code/local/Yourcompany/Youextension/Model/Notification.php
    app/code/local/Yourcompany/Youextension/Model/Observer.php
    design/adminhtml/default/default/layout/yourcompany/yourextension.xml

Config

app/code/local/Yourcompany/Youextension/etc/config.xml

As everything else, it starts in your config.xml. You'll probably already have block- and model-section defined for your extension, but I've included them here for completion.

The important part to notice is the reference to the layout file and the observer we'll setup to listen for messages:

<?xml version="1.0"?>
<config>
    <modules>
        <Yourcompany_Yourextension>
            <version>0.1.0</version>
        </Yourcompany_Yourextension>
    </modules>
    <global>
        ...
        <blocks>
            <yourextension>
                <class>Yourcompany_Yourextension_Block</class>
            </yourextension>
        </blocks>
        <models>
            <yourextension>
                <class>Yourcompany_Yourextension_Model</class>
            </yourextension>
        </models>
        <events>
            <yourextension_notifications_before>
                <observers>
                    <yourextension_observer>
                        <type>singleton</type>
                        <class>Yourcompany_Yourextension_Model_Observer</class>
                        <method>checkMessages</method>
                    </yourextension_observer>
                </observers>
            </yourextension_notifications_before>
        </events>

        ...
    <adminhtml>
        ...
        <layout>
            <updates>
                <yourextension>
                    <file>yourcompany/yourextension.xml</file>
                </yourextension>
            </updates>
        </layout>
        ...
    </adminhtml>
</config>

Layout

design/adminhtml/default/default/layout/yourcompany/yourextension.xml

In the layout file, you'll have to make a reference to the notification area. It is simply called notifications.

The <default> is the path for which this layout is to be used. <default> means everywhere.

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="notifications">
            <block type="yourextension/adminhtml_notifications" name="notifications_yourcompany" />
        </reference>
    </default>
</layout>

The block part type is string that lets Magento look up the block.

<block type="yourextension/adminhtml_notifications" name="notifications_yourcompany" />

The first part, yourextension, obviously tells it to look in your extension path: app/code/local/Yourcompany/Youextension

The second part, adminhtml_notifications, turns into: Adminhtml/Notifications.php

These two parts are glued together by Block, and there your have, viola: app/code/local/YourCompany/YourExtension/Block/AdminHtml/Notifications.php

The name in <block name="yourextension"/> just has to be unique.

app/code/local/Yourcompany/Youextension/Block/Notifications.php

In this example, the block will get the data and render the HTML directly. Normally you would include a template as well, but for this example it is not necessary.

To get the messages, we'll use an oberserver-pattern. This means that we send out a message that we about to write out the notifications. Other parts of the extension or even other extensions can opt to add messages.

This is what the Mage::dispatchEvent('yourextension_notifications_before') part below does.

If another part of the extension listens to this event and adds messages to our Notification model, then we are in luck. In fact we already know from config.xml that our Observer model will listen to this event.

So when we call getMessages() on our Notification model below, messages will have magically appeared.

The last part of the _toHtml is what renders the notifications.

class Yourcompany_Yourextension_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
{
    public function _toHtml($className = "notification-global")
    {
        // Let other extensions add messages
        Mage::dispatchEvent('yourextension_notifications_before');
        // Get the global notification object
        $messages = Mage::getSingleton('yourextension/notification')->getMessages();
        $html = null;
        foreach ($messages as $message) {
            $html .= "<div class='$className'>" . $message . "</div>";
        }
        return $html;
    }
}

Model

app/code/local/Yourcompany/Youextension/Model/Notification.php

The model is super simple for our purpose. Think of the Notification model as a container for messages rather than a single notification.

class Yourcompany_Yourextension_Model_Notification extends Varien_object
{
    protected $messages = [ ];

    public function getMessages()
    {
        return $this->messages;
    }

    public function setMessages($messages)
    {
        $this->messages = $messages;
        return $this;
    }

    public function addMessage($message)
    {
        $this->messages[] = $message;
        return $this;
    }
}

Observer (sender)

app/code/local/Yourcompany/Youextension/Model/Observer.php

The observer is the last piece of the magic. We set it up in config.xml to listen for yourextension_notifications_before. So when our block is about to render, we have the option to add a message to the Notification model first.


class Yourcompany_Yourextension_Model_Observer
{

    public function checkMessages($observer)
    {
        $notifications = Mage::getSingleton('yourextension/notification');
        $notifications->addMessage("I was sent by Yourextension");
        return $observer;
    }
}

Wrap up

So once your extension boots up, it registers Model/Observer to listen for a certain event—the event that we will then render notifications.

In this tutorial, we have created a layout that references Magento's own notification area, and on all pages, we will render our own block.

Since Model\Notification is a singleton, we can addMessage()s to it from all over our extension (and outside), and when we call getMessages() we'll get them all. We don't have to worry about storing multiple messages temporarily.

It is up to you how you want to store the messages—in session or using a model resource to save in the database.

Discover and read more posts from Michael Bøcker-Larsen
get started
post commentsBe the first to share your opinion
Mindy Yeh
7 years ago

Hi, this is a great step by step guide! I think I’ve managed to set it up, but not quite sure how to actually submit a notification to test if it works?

Michael Bøcker-Larsen
7 years ago

You can add notifications from anywhere like this:

$notifications = Mage::getSingleton(‘yourextension/notification’);
$notifications->addMessage(“I was sent by Yourextension”);

If you are looking for a way to post notifications as a user, you would have to build a module for that. You would save the notification and then feed the notification back in using the above code.

Show more replies