I want to override _getHtml() function from topmenu.php file using block. So Please help me


  • Can you please tell here what you want to perform? Jul 19 at 12:26
  • i want to simply override topmenu.php file function using block. Jul 19 at 12:31
  • override file working for you? Jul 20 at 12:32
  • 1
    i will try this thanks bro Jul 20 at 13:13

You can override block by preference in di.xml file in your module :

<?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="Vendor\Module\Block\Html\Topmenu" />

Now, Create Topmenu.php file


 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
namespace Vendor\Module\Block\Html;

use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\View\Element\Template;

class Topmenu extends Template implements IdentityInterface
    //add override code here

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.