Skip to content

Instantly share code, notes, and snippets.

@artmouse
Created May 14, 2015 07:52
Show Gist options
  • Save artmouse/33742829f9490a14dfcc to your computer and use it in GitHub Desktop.
Save artmouse/33742829f9490a14dfcc to your computer and use it in GitHub Desktop.
Messages and Global Messages Blocks
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment