0

I want override the block file. I had given the details of the original file path and my overridden file.

My overridden file

app\code\Vendor\Module\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="Sm\MegaMenu\Block\MegaMenu\View" type="Sm\MageMenuCutomize\Block\MageMenu\View" />
</config>

app\code\Vendor\Module\etc\module.xml

<?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="Sm_MageMenuCutomize" setup_version="1.0.0" schema_version="1.0.0">
    </module>
</config>

app\code\Vendor\duplicate\Block\Menu\View.php

<?php
namespace Vendor\duplicate\Block\Menu;           
class View extends Template
{
    /* My Code*/
}

My Original File

app\code\Vendor\original\Block\Menu\View.php

<?php
    namespace Vendor\original\Block\Menu;           
    class View extends Template
    {
        /* My Code*/
    }

I got the empty page after this processed is finished. What is the best way to over ride this.

Thanks in advance.

2 Answers 2

1

Following step need to follow

create app/code/Vendor/ModuleName/registration.php

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

create app/code/Vendor/ModuleName/etc/module.xml

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

create app/code/Vendor/ModuleName/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="ThirdPartyVendor\ThirdpartyModule\Block\File.php" type="Vendor\ModuleName\Block\File.php"/>
</config>

create app/code/Vendor/ModuleName/Block/File.php

namespace Vendor\ModuleName\Block;
use ThirdPartyVendor\ThirdpartyModule\Block\File as ThirdPartyFile;

class File extends ThirdPartyFile
{
...........
...........
}

And Finally Run Below Commands:

 php bin/magento setup:di:compile
 php bin/magento c:f
1

Block overriding is a bad development practice. You have to create custom layout for needed page and just replace block class where needed. Example:

<referenceBlock name="needed.block" class="Sm\MageMenuCutomize\Block\MageMenu\View">

It's obvious that your custom block have to extend original block.

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.