0

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

THANKS.

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

1 Answer 1

1

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" />
</config>

Now, Create Topmenu.php file

app/code/Vendor/Module/Block/Html/Topmenu.php

<?php
/**
 * 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 and acknowledge you have read our privacy policy.

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