remove global messages not working
-
16-10-2019 - |
Question
I need to remove global messages. I did put following to local.xml
<remove name="global_messages" />
<remove name="messages" />
but messages still appear. I also tried to comment out following lines in page.xml
<block type="core/messages" name="global_messages" as="global_messages"/>
<block type="core/messages" name="messages" as="messages"/>
which also did not work. Notice messages still appear. Where are the messages still coming from if commented out in page.xml? and Yes, I did flush Magento cash.
Solution
It doesn't work because...
For example in the product view page this template is rendered: catalog/product/view.phtml
.
This template contains at the top this line:
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
And $this->getMessagesBlock()
is defined in Mage_Core_Block_Abstract
and it looks like this:
public function getMessagesBlock()
{
if (is_null($this->_messagesBlock)) {
return $this->getLayout()->getMessagesBlock();
}
return $this->_messagesBlock;
}
This means that it calls: Mage_Core_Model_Layout::getMessagesBlock();
This last method is
public function getMessagesBlock()
{
$block = $this->getBlock('messages');
if ($block) {
return $block;
}
return $this->createBlock('core/messages', 'messages');
}
This means that if a block with the name 'messages' is not found in the layout is created and returned.
I see 2 possible solutions here:
- Edit all the template files and remove
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
. But I wouldn't take this road. It's to much time consuming and you might miss something. - Override "magento style" the method:
Mage_Core_Block_Messages::getGroupedHtml()
to return an empty string.
First I thought of overriding Mage_Core_Model_Layout::getMessagesBlock()
to return nothing if the block is not in layout, but you will get errors when calling ->getGroupedHtml()
on a non object.
but you can try overriding Mage_Core_Model_Layout::getMessagesBlock()
to return a simple core/template
block that does not have a method getGroupedHtml()
method and will return null when called. (this would be the third solution.)
OTHER TIPS
Or you can add in XML
unused block like
<block type="core/messages" name="grab_messages" />
before old blocks. Because first exemplar of core/messages
block grab all messages and clear messages from session.