Mar 16, 2015

How to add custom link in top links after my account link in Magento2

In Magento2, we can add a custom link in top links as follows, please check the steps one by one.

Consider, we wish to add a 'Test Link' after My Account link and assuming you have your custom module name as 'Mynamespace/Mymodule'.

Step 1: Create a block for your custom link  app\code\Mynamespace\Mymodule\Block\Link.php then add the code below in your block file.

Code:

 <?php  
 namespace Mynamespace\Mymodule\Block;  
 class Link extends \Magento\Framework\View\Element\Html\Link  
 {  
   public function getHref()  
   {  
     return $this->getUrl('test');  
   }  
   public function getLabel()  
   {  
     return __('Test Link');  
   }  
 }  
 ?>  


Step 2: Now create app\code\Mynamespace\Mymodule\view\frontend\layout\default.xml, if not already created and add code below in body tag

Code:

 <body>      
  <referenceBlock name="top.links">  
  <block class="Mynamespace\Mymodule\Block\Link" name="test-link" after="my-account-link"/>  
  </referenceBlock>      
 </body>  


Step 3: Clear All Cache and you are done.

No comments:

Post a Comment