Extending Magento's topLinks to Include Nested/Dropdown Child Menu's

Extending Magento's topLinks to Include Nested/Dropdown Child Menu's

September 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Extending Magento's topLinks to Include Nested/Dropdown Child Menu's

We’ve successfully extended the Magento class Mage_Page_Block_Template_Links using the method outlined by Sonassi in their knowledge base article. Let’s dive into the steps we’ve taken to achieve this.

Editing the _construct Function

First, we edited the _construct function to include our custom template:

protected function _construct()
{
    $this->setTemplate('page/template/links_nested.phtml');
}

This ensures that the template used for rendering the links is our modified version, which will handle nested child menus.

Next, we made changes to the addLink function to include the $childMenu variable and allow its usage in XML layout:

public function addLink($label, $url = '', $title = '', $prepare = false, $urlParams = array(), $position = null, $liParams = null, $aParams = null, $beforeText = '', $afterText = '', $childMenu = false)
{
    // ... (rest of the function remains unchanged)

    $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,
        'child_menu' => ($childMenu ? $this->getLayout()->getBlock($childMenu) : '')
    ));

    // ... (rest of the function remains unchanged)
}

This modification allows us to specify a child menu block in the XML layout.

Using ChildMenu Parameter in local.xml

Now, we wish to include the <childMenu> parameter in the local.xml file for an item in topLinks:

<reference name="top.links">
    <action method="addLink" translate="label title before_text">
        <label>Account</label>
        <!-- ... -->
        <position>10</position>
        <liParams>id="account-dropdown"</liParams>
        <aParams />
        <before_text />
        <after_text />
        <childMenu>account-submenu</childMenu>
    </action>
</reference>

Here, we’ve specified account-submenu as the child menu for our ‘Account’ top link.

Constructing the ChildMenu

Next, we construct the child menu as account-submenu:

<reference name="top.links">
    <block type="page/template_links" name="submenu" as="submenu">
        <action method="setName"><name>account-submenu</name></action>
        <!-- ... -->
    </block>
</reference>

This block will contain the links for our ‘Account’ submenu.

Modifying the Template File

Finally, we made some modifications to the template file (links_nested.phtml) to render childMenu if declared:

<?php $_links = $this->getLinks(); ?>
<?php if(count($_links)>0): ?>

<!-- ... (rest of the template remains unchanged until the foreach loop) -->

<?php endforeach; ?>
</ul>
<?php endif; ?>

We’ve added a line inside the foreach loop to render the child menu:

<?php echo ($_link->getChildMenu()) ? $_link->getChildMenu()->toHtml() : ''; ?>

This line checks if a child menu is assigned and, if so, renders it using the toHtml() method.

The Current Issue

Everything appears to be working as expected in the backend, but there’s an issue with rendering the child menu on the frontend. The child menu doesn’t render anything at all under the ‘My Account’ top link, which means no submenu is displayed.

The problematic line seems to be this:

<?php echo ($_link->getChildMenu()) ? $_link->getChildMenu()->toHtml() : ''; ?>

We suspect there might be an issue with how we’re calling for the child menu in the template file. The toHtml() method doesn’t seem to be rendering anything.

Conclusion

While our modifications have allowed us to specify and construct a child menu in the top links, we’re currently unable to successfully render this submenu on the frontend. There appears to be an issue with how we’re attempting to access and display the child menu in the template file.

We’ve checked that the child menu is properly declared and assigned in both the local.xml layout file and the custom block definition, but the rendering process seems to be where the problem lies.

Further investigation is needed to determine why the toHtml() method isn’t working as expected for the child menu. We’ll continue to troubleshoot this issue and post an update if we find a solution.

For more detailed Magento development guides and tutorials, feel free to explore our Magento Development category on LaravelCompany.

API Themes

Our network