2

How to set custom item in TopMenu to show DropDown menu on mouse hover I tried this code app/code/StackExchange/Menu/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Theme\Block\Html\Topmenu">
        <plugin name="stackexchange-menu" type="StackExchange\Menu\Plugin\Menu" /> <!-- declare your own plugin -->
    </type>
</config>

and in the Plugin code app/code/StackExchange/Menu/Plugin/Menu.php

<?php
namespace StackExchange\Menu\Plugin;

use Magento\Framework\Data\Tree\NodeFactory;

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

    public function __construct(
        NodeFactory $nodeFactory
    ) {
        $this->nodeFactory = $nodeFactory;
    }

    public function beforeGetHtml(
        \Magento\Theme\Block\Html\Topmenu $subject,
        $outermostClass = '',
        $childrenWrapClass = '',
        $limit = 0
    ) {
        $menu = $subject->getMenu();
        //here is how you can create a menu item.  
        $node = $this->nodeFactory->create(
                [
                    'data' => [
                    'name' => __('Brands'),
                    'id' => 'some-unique-id-here',
                    'url' => 'http://www.example.com/',
                    'has_active' => false,
                    'is_active' => false // (expression to determine if menu item is selected or not)
                ],
                'idField' => 'id',
                'tree' => $menu->getTree()
            ]
        );
        $menu->addChild($node); 

        //do the above as many times as you need.  
        //if you want to add a sumbmenu item to a menu item, create a node called `$subNode` in the same way as above and instead of calling $menu->addChild($node); call $node->addChild($subNode);  where $node is the parnet node
    }
}

now its works perfectly but the issue is that is shows like enter image description here

Now the issue is I want whenever I mouse hover on the Brands it shows me a dropdown and on dropdown shows a custom div. How can I set this?

1
  • So Sir how can I set this with the above Plugin?
    – Asad Khan
    Apr 30, 2019 at 10:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.