I want to adjust the header menu. It works a bit by editing topmenu.phtml, at least for the $outermostClass to add some classes but I think $childrenWrapClass has an error since in function _addSubMenu in topmenu.php it does not use the var.

Though anyway, I would need more than adding a class to submenus. I would need to make the links look like

<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">

So I would need to edit topmenu.php in any way.

How to edit the topmenu.php? It is located originally in module-theme/Block/Html/Topmenu.php

Can I place it in my theme directory somewhere simply?

Someone mentioned I would need to create a module for it. Does that mean creating a directory on the same level of Magento_Theme in my theme directory? If so then how to name that directory and where to place my topmenu.php in order to use this instead the original one?

Thank you!

1 Answer 1


You have to create module for changes in Topmenu.php file,

File path is : app/code

Create module name Package/Topmenu/registration.php,




<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Package_Topmenu" setup_version="1.0.0"></module>

Create di.xml file for override topmenu file,

filepath is Package/Topmenu/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">
    <preference for="Magento\Theme\Block\Html\Topmenu" type="Package\Topmenu\Block\Topmenu"/>

Create block for override topmenu file function, Package/Topmenu/Block/Topmenu.php,


namespace Package\Topmenu\Block;

class Topmenu extends \Magento\Theme\Block\Html\Topmenu
     //override core function here..


Run command , php bin/magento setup:upgrade

Clear cache and check now its overrider core block.

  • Thank you for your answer but I was wondering about some things. In my magento 2.0.7 there is only a app/design and a app/etc directory. Is the app/code path still correct or is it from some earlier magento version?
    – user789660
    Jun 22, 2016 at 11:29
  • You have to just create folder inside app folder with name code, by default code folder is not available Jun 22, 2016 at 11:31
  • Thank you! And regarding the directory names Package and Topmenu, are those to be taken in order to override or in order to easily recognize or can I use directories like vendor/themename since it is only for a theme? I wonder if it would not make sense for magento devs to include only theme modules into a theme directory directly too.
    – user789660
    Jun 22, 2016 at 11:48
  • working for you now or not? Jun 22, 2016 at 11:58
  • I'm on it. Need to do the setup:upgrade now... php tags don't need closing tags anymore? didn't know that.
    – user789660
    Jun 22, 2016 at 12:01

Your Answer

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

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