1

I Have the following PHP code and I'm trying add more then 1 link to the to nav but the code only display "Test page" and doesn't show "Test page2". I'm assuming I've coded it wrong so anyone have any suggestions?

<?php
namespace testMod\navUpdate\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Data\Tree\Node;
use Magento\Framework\Event\ObserverInterface;
class Topmenu implements ObserverInterface
{

/**
 * @param EventObserver $observer
 * @return $this
 */
public function execute(EventObserver $observer)
{
    /** @var \Magento\Framework\Data\Tree\Node $menu */
    $menu = $observer->getMenu();
    $tree = $menu->getTree();
    $data = [
        'name'      => __('Test page'),
        'id'        => 'test-page',
        'url'       => 'test-page',
    ];
    $node = new Node($data, 'id', $tree, $menu);
    $menu->addChild($node);
    return $this;
}

public function execute2(EventObserver $observer)
    {
        /** @var \Magento\Framework\Data\Tree\Node $menu */
        $menu = $observer->getMenu();
        $tree = $menu->getTree();
        $data = [
            'name'      => __('Test page2'),
            'id'        => 'test-page2',
            'url'       => 'test-page2',
        ];
        $node = new Node($data, 'id', $tree, $menu);
        $menu->addChild($node);
        return $this;
    }

}

?>
1

Well, Magento 2 observers have only one method that will get executed and it is the execute method.

So in your case only the execute() method is being executed that's why you don't get your second link from your execute2() method.

The solution is to update your execute() method with the code of your execute2 method:

public function execute(EventObserver $observer)
{
    /** @var \Magento\Framework\Data\Tree\Node $menu */
    $menu = $observer->getMenu();
    $tree = $menu->getTree();
    $data = [
        'name'      => __('Test page'),
        'id'        => 'test-page',
        'url'       => 'test-page',
    ];
    $node = new Node($data, 'id', $tree, $menu);
    $menu->addChild($node);
    $data = [
        'name'      => __('Test page2'),
        'id'        => 'test-page2',
        'url'       => 'test-page2',
    ];
    $node = new Node($data, 'id2', $tree, $menu);
    $menu->addChild($node);
    return $this;
}
  • when I try and add a third link it only displays the first and last link any ideas how I could solve this problem? – Mark May 26 '16 at 13:32
  • @user3557071 please create a new issue with the following code, it'll be easier than just talk about it in the comments – Raphael at Digital Pianism May 26 '16 at 13:33
  • forgot i created it yesterday: magento.stackexchange.com/questions/116936/… – Mark May 26 '16 at 13:35

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.