Digging into the 1.9 RWD nav menu, I am fiddling with Varien_Data_Tree
and Varien_Data_Tree_Node
– I haven't worked with these classes directly before and they seem unfinished as there are several empty methods in the classes.
Using an observer on the page_block_html_topmenu_gethtml_before
event, I am able to add a new item to the menu using:
$event = $observer->getEvent();
$menu = $event->getMenu();
$nodeData = array(
'name' => 'My Node',
'id' => 'my_node',
'url' => 'My-Url.html',
'is_active' => false
);
$node = new Varien_Data_Tree_Node($nodeData, 'my-node', new Varien_Data_Tree());
$menu->addChild($node);
As expected, I see a new nav menu item on the front-end listed after any other nav items already built; however, I would like to position this node so that it is shown before or after any other items (such as how before="-"
after="-"
are used in layout XML).
How can I adjust the position of items added to the nav menu in this fashion?
Best Answer
Marius to the rescue! Its not an elegant solution but seems to be the only approach. Since its a matter of getting the existing tree items, temporarily storing the data, removing them and then readding the existing items with the newly inserted one before.
Taken from: https://magento.stackexchange.com/a/7361/69
Also it may be worth mentioning that Varien_Data_Tree_Node_Collection implements ArrayAccess.