0

I am trying to add links to my top menu using a collection - I realised I had to do this in the observer so I set it up and it is properly working (if I delete all the code inside nothing prints)

The problem is it is only adding the last item in my lists (I have two lists, one for the elements visible in the menu, one for child elements that you can see if you hover over the parent)

I have a feeling that somehow I am overwriting my last recursions but I don't see how.

Here is my code:

public function addItemsToTopmenuItems($observer)
{
    $menu = $observer->getMenu();
    $tree = $menu->getTree();

    $collection = Mage::getModel('sean_menucreator/menuelement')->getCollection();
    $menuelementNodeId = 'menuelement';
    foreach($collection as $menuitem) {

        if($menuitem->getLevel()=='1'){
            $node = new Varien_Data_Tree_Node(array(
                'name'   => $menuitem->getName(),
                'id'     => $menuelementNodeId,
                'url'    => $menuitem->getUrl(), // point somewhere
            ), 'id', $tree, $menu);

            $menu->addChild($node);

            $parent_id = $menuitem->getEntity_id();


            $action = Mage::app()->getFrontController()->getAction()->getFullActionName();
            foreach ($collection as $menuitem){
                if(isset($parent_id) && $menuitem->getParent_id() == $parent_id) {
                    $tree = $node->getTree();
                    $data = array(
                        'name' => $menuitem->getName(),
                        'id' => $menuelementNodeId,
                        'url' => $menuitem->getUrl(),
                        'is_active' => ($action == 'sean_menucreator_menuelement_index' || $action == 'sean_menucreator_menuelement_view')
                    );

                    $menuelementNode = new Varien_Data_Tree_Node($data, 'id', $tree, $node);
                    $node->addChild($menuelementNode);
                }
            }
        }
    }
    return $this;
}

You can see it goes through the collection and picks out the elements that belong in the top level of the menu (1) and adds them, then goes through and adds child elements of that parent element based on parent_id then does it again until it has been through all of the elements in the array and checked if they belong in the menu visibly and whether they have children.

but on my menu I only see one parent element in the menu where there should be two, and the parent element only has one child where there should be two, and both of these elements are the last in their respective lists so it might be overwriting somewhere but I can't see it, can anyone help?

  • not sure if this is it, but all your elements have the same id: $menuelementNodeId. Which is always the same string menuelement. Try to make this dynamic. Add the id of the menu item at the end of it and see what happens. So instead of having 'id' => $menuelementNodeId, you can have something like 'id' => $menuelementNodeId.'_'.$menuItem->getId(), – Marius Jan 4 '16 at 15:21
  • An other thing may be the fact that you are looping through the $collection object twice. Once inside the other. – Marius Jan 4 '16 at 15:23
  • It was the static 'id', thank you lots! if you put your answer as an actual answer I will accept :) I feel like looping inside collection twice shouldn't matter? PHP should be able to handle nested foreach? – seanyt123 Jan 4 '16 at 15:27
  • I added it as an answer. Not sure about looping twice. It was just a shot in the dark. – Marius Jan 4 '16 at 15:29
1

All your elements have the same id: $menuelementNodeId.
Which is always the same string, menuelement.
Try to make this dynamic. Add the id of the menu item at the end of it and see what happens.
So instead of having 'id' => $menuelementNodeId, you can have something like 'id' => $menuelementNodeId.'_'.$menuItem->getId(),

  • Worked a charm, thanks dude :) Couldn't spot this for hours! – seanyt123 Jan 4 '16 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.