1

In general, I handle exceptions and display success or error messages within controllers. For example:

 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("The cache storage has been flushed."));

But in some cases I'd love to be able to render an error message from within a block. For example, I have blocks within my page that are dependent upon some configuration which isn't set properly currently, so what I want to do is not render those blocks but still allow other blocks around them to render.

In some cases, the other elements on the page are necessary in order to fix the problem that's causing the exception in the first place.

But I think the messages block is initialized very early in the layout rendering process, so by the time you're in a block, if you try to set a message on the session, it won't display until next page load, which would be pretty confusing of course.

I searched the core and I don't see any instance of addError() within Mage/Adminhtml/Block/

I did stumble onto this little gem, but it doesn't seem to work as I'd expect:

$this->getLayout()->getMessagesBlock()->addError('oh what');
6

You can do this by overwriting the _prepareLayout() function in your block:

protected function _prepareLayout() {
    // IF statement check for config  or something else
        $this->getMessagesBlock()->addError('oh what');
    // End of IF
    return parent::_prepareLayout();
}

_prepareLayout() is called before all of the _toHtml() functions are called on every block and after the layout has been loaded.

  • Thanks Rick! That looked SO promising, but I just tested it and it didn't work. The session message didn't show until subsequent page load. – kalenjordan Jan 25 '13 at 18:39
  • @sparcksoft Could you show us some of the code? I'll try and reproduce it. – Rick Kuipers Jan 25 '13 at 18:43
  • @sparcksoft Ha! You had the answer all along, I changed my code above ^. This works for me. – Rick Kuipers Jan 25 '13 at 18:54
  • Awesome! I know that I had tried this before and it didn't work, but I didn't try it in the _prepareLayout() method, I just tried it somewhere within my block logic. Currently with the block I'm testing in, it's working whether or not I do it in the _prepareLayout(). – kalenjordan Jan 25 '13 at 21:59
2

I'm not sure, wether I understand you correct.

You can get the Message Block via

Mage_Core_Block_Abstract::getMessagesBlock()
// means $this in block context (template and Block class)

there you can everything you want, for example add new errors:

$this->getMessagesBlock()->addError('Configuration is wrong!');

or show the error messages with your own template:

$this->getMessageBlock()->setTemplate('my_template.phtml');
echo $this->getMessageBlock()->toHtml();

Have a look on the following methods to change the layout of the messages:

Mage_Core_Block_Messages::setMessagesFirstLevelTagName()
Mage_Core_Block_Messages::setMessagesSecondLevelTagName()
  • I'm not sure where the messages are deleted. because of this, it might be too late in the template to do something. – Fabian Blechschmidt Jan 25 '13 at 19:30
  • Ya when I tried $this->getMessagesBlock()->addError('Configuration is wrong!'); it didn't work. I didn't look too closely at the order of execution though to see if that was the problem. – kalenjordan Jan 25 '13 at 21:56
  • what do you mean with didn't work? If you didn't unset the message block, you have on every page one. The question is, is it rendered. – Fabian Blechschmidt Jan 26 '13 at 9:45
  • When I used it before (outside of _prepareLayout()), the error message wasn't rendered until the next page load. Currently this is working for me as I mentioned in the @Rick's answer. – kalenjordan Jan 26 '13 at 19:54
  • if you use addError() in your template and afterwards echo $this->getMessageBlock()->toHtml(); you should get the error. but the block in the head of the HTML document is already rendered, therefore you can't add new errors in the template to the head, that's right. – Fabian Blechschmidt Jan 26 '13 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.