1

I Have Followed Follwing Article From Stack Exchange For Adding External Link To Magento Navigation Also Referred Magento 2 Repo. And Created The Plugin. But Its Not Adding Link To My Navigation Bar.

0

Please create the plugin using the following steps:

Module's di.xml

 <type name="Magento\Theme\Block\Html\Topmenu">
    <plugin name="modulename-topmenu" type="Namespace\Modulename\Block\Topmenu\Plugin" />
 </type>

Plugin Class

namespace Namespace\Modulename\Block\Topmenu;

use Magento\Framework\Data\Tree\NodeFactory;

    class Plugin
    {

        protected $nodeFactory;
        protected $urlInterface;

        public function __construct(
            NodeFactory $nodeFactory,
            \Magento\Framework\UrlInterface $urlInterface
        ) {
            $this->nodeFactory = $nodeFactory;
            $this->urlInterface = $urlInterface;
        }

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

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

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.