0

I managed to add Custom Link1 to a CMS page to the main catalog navigation following the steps from the below link. There it explains how to add one custom link but I need to add two links and I don't know how to get it working so it shows both links.
Magento 2 How to add Home link in navigation bar!
Here is part of my Topmenu.php. Can anyone please help? Thanks

/**
     *
     * Build node
     *
     * @throws NoSuchEntityException
     */
    protected function getNodeAsArray()
    {
        return [
            'name' => __('Custom Link1'),
            'id' => 'custom-link1',
            'url' => 'custom-link1',
            'has_active' => true,
            'is_active' => true
        ];
        [
            'name' => __('Custom Link2'),
            'id' => 'custom-link2',
            'url' => 'custom-link2',
            'has_active' => true,
            'is_active' => true
        ];
    }

0

You can use like this below way :

<?php

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\Plugin;

use Magento\Framework\Data\Tree\NodeFactory;
use Magento\Framework\UrlInterface;

class Topmenu {
    /**
     * @var NodeFactory
     */
    protected $nodeFactory;

    /**
     * @var UrlInterface
     */
    protected $urlBuilder;

    /**
     * @param NodeFactory  $nodeFactory
     * @param UrlInterface $urlBuilder
     */
    public function __construct(
        NodeFactory $nodeFactory,
        UrlInterface $urlBuilder
    ) {
        $this->nodeFactory = $nodeFactory;
        $this->urlBuilder = $urlBuilder;
    }

    public function beforeGetHtml(
        \Magento\Theme\Block\Html\Topmenu $subject,
        $outermostClass = '',
        $childrenWrapClass = '',
        $limit = 0
    ) {
        /**
         * Parent Menu
         */
        $menuNode = $this->nodeFactory->create(
            [
                'data' => $this->getNodeAsArray("Main Menu", "main-menu"),
                'idField' => 'id',
                'tree' => $subject->getMenu()->getTree(),
            ]
        );
        /**
         * Add Child Menu
         */
        $menuNode->addChild(
            $this->nodeFactory->create(
                [
                    'data' => $this->getNodeAsArray("Sub Menu", "sub-menu"),
                    'idField' => 'id',
                    'tree' => $subject->getMenu()->getTree(),
                ]
            )
        );
        $subject->getMenu()->addChild($menuNode);
    }

    protected function getNodeAsArray($name, $id) {
        $url = $this->urlBuilder->getUrl($id);
        return [
            'name' => __($name),
            'id' => $id,
            'url' => $url,
            'has_active' => false,
            'is_active' => false,
        ];
    }
}

I added Parent Menu & Child Menu. You can use it multiple times which you want to add multiple links.

Reference

0

You can try the code like this one.

public function beforeGetHtml(
        \Magento\Theme\Block\Html\Topmenu $subject,
        $outermostClass = '',
        $childrenWrapClass = '',
        $limit = 0
    ) {
        $node = $this->nodeFactory->create(
            [
                'data' => $this->getNodeAsArray(),
                'idField' => 'id',
                'tree' => $subject->getMenu()->getTree()
            ]
        );
        $subject->getMenu()->addChild($node);

        $node2 = $this->nodeFactory->create(
            [
                'data' => $this->getNodeAsArray2(),
                'idField' => 'id',
                'tree' => $subject->getMenu()->getTree()
            ]
        );
        $subject->getMenu()->addChild($node2);
    }

    /**
     *
     * Build node
     **/
    protected function getNodeAsArray()
    {
        return [
            'name' => __('Home'),
            'id' => 'home',
            'url' => '/',
            'has_active' => true,
            'is_active' => true
        ];
    }

    protected function getNodeAsArray2()
    {
        return [
            'name' => __('Custom Link2'),
            'id' => 'yourID',
            'url' => 'yourURL',
            'has_active' => true,
            'is_active' => true
        ];
    }

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.