Override Topmenu Block in Magento 2

Override Topmenu Block in Magento 2

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Override Topmenu Block in Magento 2

Overriding the Topmenu Block in Magento 2

If you’re trying to override the Topmenu block in Magento 2 to change the HTML structure of your sub menus, you’ve come to the right place. In this guide, I’ll walk you through the steps necessary to achieve this.

Understanding the Problem

The issue here is that despite creating a module and a plugin for the Topmenu block, the override isn’t working as expected. The error suggests that the _menu object in your plugin is null, which means the Magento framework hasn’t recognized your customization attempt.

The Step-by-Step Solution

1. Create Your Module

First, create a new module directory under app/code/Ecommerce/TopMenu. Ensure you have the necessary files and directories:

- Ecommerce
  - TopMenu
    - etc
      - di.xml
      - module.xml
    - Plugin
      - Topmenu.php
    - registration.php

2. Define Dependencies

In registration.php, define your module’s dependencies:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Ecommerce_TopMenu',
    __DIR__
);

This tells Magento where to find your module.

3. Define the Layout XML

In etc/module.xml, define the layout file that will call your block:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Ecommerce_TopMenu" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Theme"/>
        </sequence>
    </module>
</config>

This ensures your block appears in the layout.

4. Create the Topmenu Plugin

In Plugin/Topmenu.php, create a plugin for the top menu:

<?php

namespace Ecommerce\TopMenu\Plugin;

class Topmenu
{
    public function aroundGetHtml($subject, callable $proceed)
    {
        // Add your customization logic here
        return $proceed();
    }
}

The around keyword wraps the original method with a custom implementation.

5. Define the Plugin Configuration

In etc/di.xml, configure your plugin:

<?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="ecommerce_topmenu_plugin" type="Ecommerce\TopMenu\Plugin\Topmenu"/>
    </type>
</config>

This tells Magento to inject your plugin into the Topmenu block.

The Common Mistake

The most common mistake is forgetting to define the dependencies correctly. In this case, you need to ensure that your module’s XML files are properly configured and that all necessary files are in place. Additionally, make sure you’ve run bin/magento setup:upgrade and cleared the cache (bin/magento cache:flush) after making any changes.

Conclusion

Overriding a Magento block requires careful configuration of your module structure and XML files. By following this guide, you should be able to successfully override the Topmenu block and customize its output markup. If you’re still having trouble, don’t hesitate to reach out for further assistance.

For more detailed guides on Magento development and best practices, I recommend checking out LaravelCompany, a leading resource for learning about Laravel and other PHP frameworks.

Magento 2 Extensions Themes Configuration

Our network