28 January, 2023

How To Add Or Edit Top Links Sections In Magento

Links Section

If ever you are confused about how to add / edit / remove the top links from links section in magento, then this blog post would certainly help you by illustrating the exact process step by step. You may be blinking what is meant by the top links mentioned here. Just check out below to see the top links in header: My Account, My WishList ,Login, My Cart ,Checkout.

Top links in magento are managed in two different ways:

  • By using the Layout XML files.
  • By adding custom function.

Let us, first see how to add the new link in the existing top links section with help of the layout XML file. First we will see the basic syntax.

<reference name=”top.links”><action method=”addLink” translate=”label title” module=”<module name>” ifconfig=”<config path value to check module enabled> “><label><your label name></label><url><Link></url><title><Link Title></title><prepare><Boolean></prepare><urlParams><Pa rams value></urlParams> <position><position to display></position><liParams><Array value></liParams> <aParams><class name></aParams><beforeText></beforeText><afterText></afterText></action></reference>So, here we are taking the reference of “top.links” block, and calling the “addLink” function on that block. The “top.links” block is defined in page.xml<block type=”page/template_links” name=”top.links” as=”topLinks”/>

Now we are going to see the use of every element tag in the above syntax.

<action method=”addLink” translate=”label title”>method =‘addLink’ function is defined in class Mage_Page_Block_Template_Links. translate=”label title” label get translated based on the our site languageother attributes are module and ifconfig , which are not mandatory.Module is assigned as the module name, i.e module=”<your module name>”where the ifconfig checks whether the module is enabled /disabled, i.e ifconfig=”<Admin enable/disable field value>”, for example for contact us link ifconfig =”contacts/contacts/enabled”
<label/>Front end display name, for example ‘My Account ’ is label in the top links section
<url/>can use full url also, here is an example <url helper = “customer/getAccountUrl”></url>here we can pass the controller/helper function link with the help of the “helper” attribute
<title/>This displays the title of the link when we move the mouse over the link
<prepare/>Need to declare the Boolean value “true/false” either to adding base url.param
<urlParams/>there are a few param you can send to do different things in <urlParams/>, here is  the file path app/code/core/Mage/Core/Model/Url.php.
<position/>Position in the default list of linksThis tag helps specify the position, by passing the numeric value as following <position>120</position>
<liParams/>By Default we pass the value as NULL parameters for your li. Class will only work if it’s not the first or last item, because class is automatically set. Example “<liParams><id>myLi</id></liParams>”
<aParams/>parameters for your <a>, also can pass css class ,e.t.cExample <aParams><rel>nofollow</rel></aParams>
<beforeText/>It takes the Null value by default
<afterText/>It takes the Null value by default

Mandatory tags that need to pass with in the <action> are as follows ,<label>,<url>,<title>,<Prepare> and <Position>

Note:

  • In the top links, when you add item to your wishlist or cart, the links My Cart and My Wishlist change to My Cart(2) and My Wishlist(2). So you need to keep this in mind, if you are making any changes to these top links.
  • The Login link turns into a Logout link, so if you make any changes to the Login link you will have to do the same for the Logout link.
  • Few param you can send to do different things in <urlParams>, let us see in brief about it.
<_direct>about-us.html</_direct><!–if you’re not going to a directory, and it is a prepared url, you need this to be set –><_fragment>abc</_fragment><!– places a #abc at the end of the url –><!– <_escape>true</_escape> –><!– Not Really Used, as far as I can tell. It decides whether or not to escape the & to &amp; since it’s XML, it already does. –><!– <_nosid>true</_nosid> –><!– Also, not known. –><_query><a>1</a><b>2</b></_query><!– adds a query-string for your url. This adds ?a=1&amp;a=2 to your url.

 Now we will see a small example  on how to add new the top links in the layout xml file.

<reference name=”top.links”><action method=”addLink” translate=”label title” module=”testmodule”><label>My Link</label><url helper=” testmodule/gettestmoduleUrl”/><title>My Link</title><prepare/>//here are not passing any params Prepare will be Null<urlParams/>//Not passing any date in the url<position>100</position> </action></reference>

 Adding custom function in new module to add in top Links section

First we’ve seen how to add the top link in layout XML file, now we will see to create custom function, i.e nothing but method. To call in <action>, by default we use addLink().

Checkout : How to Edit Packing Slips in Magento?

Now I am going to create new method for my module to add in the top links. Here are the steps to follow:

Step 1: Create a new custom module. If you want to display already existing custom module, then go to the below path in your custom module to add new method for calling from module layout XML file.

File path:\app\code\local\Example\Testmodule\Block\Testmodule.php

<?php class Example_Testmodule_Block_Testmodule extends Mage_Core_Block_Template {//Add Testmodule link to parent block//@return Example_Testmodule_Block_Testmodulepublic function addTestLink() {$parentBlock = $this->getParentBlock(); //Create an object for getParentBlock() method//verify the module is enabled in the backend moduleEnabled()if ($parentBlock && $this->helper(‘testmodule’)->moduleEnabled()) {$text = $this->__(‘Testmodule’);//Top link Display Text$url = ‘testmodule’;$position = 120;// @param string $text//@param string $url//@param string $text// @param boolean $prepare//@param array $urlParams//@param int $position//@return Mage_Page_Block_Template_Links

$parentBlock->addLink($text, $url , $text, $prepare=true, $urlParams=array(), $position , null, ‘class=”top-link-testmodule”‘);

}

return $this;

}

} ?>

 Step 2: Go to below file path for adding the helper function to check the module enabled.

File Path:\app\code\local\Example\Testmodule\Helper\Data.php

<?phpclass Example_Testmodule_Helper_Data extends Mage_Core_Helper_Abstract {//Module configuration valueConst XML_PATH_MODULE_STATUS_ENABLE = ‘testmodule/activate/testmodule_enable’;//Verify the module is enabled in the backend//@return booleanpublic function moduleEnabled() {return Mage::getStoreConfig(self::XML_PATH_MODULE_STATUS_ENABLE);}}?>

 Step 3: Finally, we are going to add in our module layout XML file. Here is the path for module layout XML File.

File Path:\app\design\frontend\default\default\layout\testmodule.xml

Add this below code after the <layout version=”0.1.0″><default><!– Add TestModule Top link–><reference name=”top.links”><block type=”testmodule/testmodule” name=”testmodule_link” ><action method=”addTestLink()“></action></block></reference></default>

In the layout file we no need to pass any element tag such as label, url, title, prepare, urlParams, position, liparams, aparams, beforeText and after Text, because we have already passed with in the method addTestLink().

Share

Swamy is an expert Magento Developer with 3+ years of hands-on experience in developing plugins, themes. He is also interested in contributing posts on different topics of Magento-related issues and bug fixing tips.

3 Comments

  1. Aasha Reply

    Thanks for the information. It was very helpful!
    As you mentioned, we can add class for li using liParams, but class will only work if it’s not the first or last item, because class is automatically set.
    In my case, I have only two links, so these will definitely be first and last items. I would like to append some new class to these elements. Is it possible? If so, can you please suggest the necessary changes.

  2. Lorenza Minix Reply

    Awesome! Its truly remarkable article, I have
    got much clear idea regarding the link replacing and to set link sections int the controller using layout XML from this post.

Leave a Reply

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