How to Reorder your Magento Footer Links and A Walkthrough the Magento Layout/Template System

May 25, 2011

MagentoI have built a number of Magento sites and have managed to avoid the awkward question of setting the order of the footer links, here I describe how it can be done with some extra explanation into how Magento makes it all happen.

First let me explain where the links are in the Magento structure, so you can add, edit or remove them from the footer links. The links are added into the page in the template/page/html/footer.phtml file, where there are two links – usually your template will have one called footer_links and a second one called something like cms_footer_links. These are then referenced separately in the layout XML – in this case the page.xml controls it.

Below is the code from the footer.phtml that references these two blocks.

echo $this->getChildHtml('cms_footer_links');
echo $this->getChildHtml('footer_links');

These child Html blocks are expected to be mentioned in the block that calls the footer.phtml file. If they are missing, no problem – it just doesn’t add content – that is how Magento can show/hide content depending on the page you are on, or the template you are using. The relevant section from the page.xml is shown below.

<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
   <block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
      <label>Page Footer</label>
      <action method="setElementClass"><value>bottom-container</value></action>
   </block>
   <block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml"/>
   <block type="page/switch" name="store_switcher" as="store_switcher" template="page/switch/stores.phtml"/>
</block>

Now you will notice that the cms_footer_links are not referenced in the page.xml. This is because adding content from the cms is logically the job of the cms module (a standad Magento module). So, instead of page.xml we look in cms.xml where we will find the following reference to the footer that adds the cms_footer_links and specified which static block to load from the cms.

<reference name="footer">
   <block type="cms/block" name="cms_footer_links" before="footer_links">
      <action method="setBlockId"><block_id>footer_links</block_id></action>
   </block>
</reference>

The links in the CMS can be manually edited but the footer_links use a block called page/template_links and specify the template page/template/links.phtml which is a generic bit of phtml that displays a list of links based on the links provided by the block.

The page/template_links block loads its links based on calls to addLink from within references to the footer_links. Or another block – it is actually the same process for other sets of links, such as the topLinks that contain “My Account”, “Login” etc. That sounds complicated but really it isn’t. Take a look at the code below to see how the contact us link is added from within the contacts.xml layout file.

<reference name="footer_links">
   <action method="addLink" translate="label title" module="contacts" ifconfig="contacts/contacts/enabled">
      <label>Contact Us</label>
      <url>contacts</url>
      <title>Contact Us</title>
      <prepare>true</prepare>
   </action>
</reference>

There are other similar links in other layout files, such as the sitemap link in the catalog.xml and the search terms and advanced search in the catalogsearch.xml layout files.

There is a built-in way or ordering these links – simply add the position element to the addLink action. Look out through, because it seems that the elements have to be in the correct order, as well as have the correct parameter name. Simply adding the position element to the end probably won’t work because there are other, optional parameters before it. How do I know this? I checked the actual module that collates all of the links and sorts them. As already mentioned it is the page/template_links block that does the work here, which can be found in /app/code/core/Mage/Page/Block/Template/Links.php and contains the following method.

public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(),
       $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='') {

   if (is_null($label) || false===$label) {
      return $this;
   }
   $link = new Varien_Object(array(
      'label'         => $label,
      'url'           => ($prepare ? $this->getUrl($url, (is_array($urlParams) ? $urlParams : array())) : $url),
      'title'         => $title,
      'li_params'     => $this->_prepareParams($liParams),
      'a_params'      => $this->_prepareParams($aParams),
      'before_text'   => $beforeText,
      'after_text'    => $afterText,
   ));

   $this->_links[$this->_getNewPosition($position)] = $link;
   if (intval($position) > 0) {
       ksort($this->_links);
   }

   return $this;
}

You can see in the method declaration the prepare and urlParams parameters are before the position parameter but are optional. To ensure the position parameter works you need to send empty elements through for these optional parameters.

<action method="addLink" translate="label title" module="contacts" ifconfig="contacts/contacts/enabled"><label>Contact Us</label><url>ask-us-a-question</url><title>Contact Us</title><prepare>true</prepare><urlParams /><position>20</position></action>

And that’s about it, I hope it helps people out – certainly I learned a lot about how Magento works while I figured all of this out. For extra credit take a look at how the checkout and cart links are added using the checkout/links module to call addLink with some default parameters.

9NUQYJ36SNAF