1

I would like to know if this is possible to enable HTML links in custom messages (ie: error messages).

Here is my example: I've made an override for my needs of Mage_CatalogInventory_Model_Stock_Item/Item.php

Function checkQuoteItemQty :

if (!$this->checkQty($summaryQty) || !$this->checkQty($qty)) {
     //$message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available.', $this->getProductName());
     $message = Mage::helper('cataloginventory')->__('The requested quantity for "%s" is not available (max:%s).', $this->getProductName(), ($this->getQty() * 1));
     $cat_id = $this->getProduct()->getCategoryIds();
     if($cat_id){
         $url = Mage::getModel('catalog/category')->load($cat_id[0])->getUrl();
         $message .= Mage::helper('cataloginventory')->__('You might be interested in <a href="%s">those products</a>.', $url);
      }
      $result->setHasError(true)
           ->setMessage($message)
           ->setQuoteMessage($message)
           ->setQuoteMessageIndex('qty');
     return $result;
}

But the HTML link I created in $message : ($message .= Mage::helper('cataloginventory')->__('You might be interested in those products.', $url);) is not clickable and considered as text (because of the translation I guess...). Is it possible to change this behavior?

Regards.

1

For those who would like to know, I had to override Mage_Core_Block_Messages, line 249 :

public function getGroupedHtml()
    {
        $types = array(
            Mage_Core_Model_Message::ERROR,
            Mage_Core_Model_Message::WARNING,
            Mage_Core_Model_Message::NOTICE,
            Mage_Core_Model_Message::SUCCESS
        );
        $html = '';
        foreach ($types as $type) {
            if ( $messages = $this->getMessages($type) ) {
                if ( !$html ) {
                    $html .= '<' . $this->_messagesFirstLevelTagName . ' class="messages">';
                }
                $html .= '<' . $this->_messagesSecondLevelTagName . ' class="' . $type . '-msg">';
                $html .= '<' . $this->_messagesFirstLevelTagName . '>';

                foreach ( $messages as $message ) {
                    $html.= '<' . $this->_messagesSecondLevelTagName . '>';
                    $html.= '<' . $this->_messagesContentWrapperTagName . '>';
                    $html.= ($this->_escapeMessageFlag) ? $this->escapeHtml($message->getText()) : html_entity_decode($message->getText());
                    $html.= '</' . $this->_messagesContentWrapperTagName . '>';
                    $html.= '</' . $this->_messagesSecondLevelTagName . '>';
                }
                $html .= '</' . $this->_messagesFirstLevelTagName . '>';
                $html .= '</' . $this->_messagesSecondLevelTagName . '>';
            }
        }
        if ( $html) {
            $html .= '</' . $this->_messagesFirstLevelTagName . '>';
        }
        return $html;
    }

Adding a html_entity_decode at this line :

$html.= ($this->_escapeMessageFlag) ? $this->escapeHtml($message->getText()) : html_entity_decode($message->getText());

Just let me know if you find a better solution.

1
  • I found the addition of html_entity_decode unnecessary. All I needed to add to the relevant controller action was: $this->getLayout()->getBlock('messages')->setEscapeMessageFlag(false);
    – Graham
    Oct 19 '15 at 19:37

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.