Magento2 – Add Multiple Custom Links to Main Navigation via Plugin

category-navigationlinkmagento2

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
        ];
    }

Best Answer

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

Related Topic