Back to Top

How to use complex messages in Magento 2

Updated 17 April 2023

Hello Guys, We are going to learn about the Complex messages in Magento 2.

By default, when you call $this->messageManager->addSuccessMessage within Magento, you cannot pass custom HTML into your message. This doesn’t provide you with the flexibility to provide customized messages to your visitors.

However, these functions strip tags, preventing the anchor to appear. So we can use the Complex message to show the anchor.

If you don’t know how to create a custom module so you can refer to this blog for a custom module-> How to Create Hello World Module in Magento 2

Searching for an experienced
Magento Company ?
Find out More

Step 1

Add the below code in etc/frontend/di.xml. (change myexample and Vendor_Module to what you need)

<type name="Magento\Framework\View\Element\Message\MessageConfigurationsPool">
    <arguments>
        <argument name="configurationsMap" xsi:type="array">
            <item name="myexample" xsi:type="array">
                <item name="renderer" xsi:type="const">\Magento\Framework\View\Element\Message\Renderer\BlockRenderer::CODE</item>
                <item name="data" xsi:type="array">
                    <item name="template" xsi:type="string">Vendor_Module::messages/linked.phtml</item>
                </item>
            </item>
        </argument>
    </arguments>
</type>

Step 2

Created a phtml file as mentioned in the di.XMl file Vendor_Module::messages/linked.phtml

<?php echo $block->escapeUrl($block->getData('pre_link_text'))?>
<a href="<?php echo $block->escapeUrl($block->getData('url'))?>"><?php echo $block->escapeHtml(__($block->getData('link_text'))) ?></a>
<?php echo $block->escapeUrl($block->getData('post_link_text'))?>

Step 3

Now you have to call the addComplexErrorMessage() with the given name.

$this->_messageManager->addComplexErrorMessage('myexample',
            [
                'pre_link_text'=>'This is the Error Message',
                'url' => 'https://example.com',
                'link_text'=>'click here'
            ]);
            
    $this->_messageManager->addComplexSuccessMessage('myexample',
            [
                'pre_link_text'=>'This is the Success Message',
                'url' => 'https://example.com',
                'link_text'=>'click here'
            ]);   

     $this->_messageManager->addComplexNoticeMessage('myexample',
            [
                'pre_link_text'=>'This is the Notice Message',
                'url' => 'https://example.com',
                'link_text'=>'click here'
            ]);

Output

Screenshot-from-2023-04-13-16-38-24-1

As you can see that we have used the complex messages in Magento 2 in the custom module.

Happy Coding!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home