I want to create a different menu for a new theme with different html structure.

closed as unclear what you're asking by Khoa TruongDinh, Murtuza Zabuawala, Siarhey Uchukhlebau, Keyul Shah, Raphael at Digital Pianism Apr 18 '17 at 7:29

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


I have found the solution so I am answering it.

  • Menu content in M2 is generated from Block - vendor/magento/module-theme/Block/Html/Topmenu.php

  • To make changes in HTML structure, we need to override the Block in our custom Package Module say app/code/Mypackage/Mymodule/Block/Html/Topmenu.php

    Sample code to override

    namespace Mypackage\Mymodule\Block\Html;
    class Topmenu extends \Magento\Theme\Block\Html\Topmenu
        // your custom methods
  • Override above file through app/code/Mypackage/Mymodule/etc/di.xml:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <preference for="Magento\Theme\Block\Html\Topmenu" type="Mypackage\Mymodule\Block\Html\Topmenu" />
  • Make desired changes in the HTML of Menu and css as needed.

  • 1
    Could you explain what goes into the di.xml ? – Stephen Dance May 18 '18 at 15:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.