Text or Call 303.473.4400
Select Page

So, in this article I’ll give you a handy script to add a home link to the top menu.

In magento 1.7, the menu is now created via an *event* (page_block_html_topmenu_gethtml_before)

The catalog observer hooks into this and adds menu items as Varien_Data_Tree_Node objects.

I have some samples here to show you how to add a link at the beginning of this menu.

config.xml portion:

Copy The Code:


===============================
<pre>
<config>
….
<frontend>
<events>
<page_block_html_topmenu_gethtml_before>
<observers>
<catalog_add_topmenu_items>
<class>cp_layout/observer</class>
<method>addTopMenuItems</method>
</catalog_add_topmenu_items>
</observers>
</page_block_html_topmenu_gethtml_before>
</events>
</frontend>
</config>

</pre>

observer portion:
=================================
<pre>
<?php
Class Cp_Layout_Model_Observer
{
/**
* Adds additional links to the top menu
*
* @param Varien_Event_Observer $observer
*/
public function addTopMenuItems(Varien_Event_Observer $observer)
{
$nodeId = “home”;
$menu = $observer->getMenu();
$tree = $observer->getMenu()->getTree();

$data = array(
“name” => “Home”,
“id” => $nodeId,
“url” => Mage::getBaseUrl(),
“is_active” => Mage::getSingleton(‘cms/page’)->getIdentifier() == ‘home’ && Mage::app()->getFrontController()->getRequest()->getRouteName() == ‘cms’
);
$homeNode = new Varien_Data_Tree_Node($data, ‘id’, $tree, $menu);
$this->_prependNode($homeNode, $menu);

}

protected function _prependNode($node, $menu)
{
$menu->addChild($node);
$nodeId = $node->getId();
$readded = array();
foreach($menu->getChildren()->getNodes() as $n)
{
if ($n->getId() != $nodeId)
{
$readded[] = $n;
$menu->getChildren()->delete($n);
}
}
foreach($readded as $r)
{
$menu->getChildren()->add($r);
}
}
}
</pre>

Pin It on Pinterest

Share This