Messages and Global Messages Blocks

Posted Over 11 years ago. Visible to the public.

Both use the same block, as we can see in page.xml:

<block type="core/messages" name="global_messages" as="global_messages"/>
<block type="core/messages" name="messages" as="messages"/>

When you add a message, you add it to the session, rather than to either of the
messages blocks, so the code below causes the Mage_Customer_Model_Session object
to have an error logged to it:

Mage::getSingleton('customer/session')->addError('Please enter all required information');

At this point neither message blocks know anything about the message, this is
where the difference between the global messages and the messages block comes
in - it's purely down to what gets inserted into the relevant message block.

For the global messages block, I'm not sure how it gets filled actually, I can't
find any reference in the code at all to something interacting with the block.

For the messages block, it gets filled by you using _initLayoutMessages() from
your controller, like so:

$this->_initLayoutMessages('customer/sesssion');

What this does is basically the following, which is a simplification from
Mage_Core_Controller_Varien_Action::_initLayoutMessages():

$storage = Mage::getSingleton('customer/sesssion');
$block = $this->getLayout()->getMessagesBlock();
$block->addMessages($storage->getMessages(true));

A side note - the Magento templates use this method call to render the message
block:

$this->getMessagesBlock()->getGroupedHtml();

This stops the Enterprise Full Page Cache from working, instead this should be
used:

$this->getMessagesBlock()->toHtml();

In summary - it looks like the global messages block is not used at all. If
anyone knows any better, let me know!

Mike Whitby
Last edit
Over 10 years ago
Posted by Mike Whitby to Magento (2012-08-17 10:14)