I tried to override block file but I unable to see the output.

my script

di.xml file:

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

Block file:


namespace Company\Cms\Block\Html;

class Topmenu extends \Magento\Theme\Block\Html\Topmenu{

    protected function _getHtml(
        \Magento\Framework\Data\Tree\Node $menuTree,
        $colBrakes = []
    ) {
        echo "exxit"; exit;

and I followed the below link for this

How to override blocks in v2.1

  • please let me know if you have issue Nov 14 '17 at 13:10

You need to change below way for your preference,

<?xml version="1.0" encoding="UTF-8"?>
<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="Takeuchi\Cms\Block\Html\Topmenu" />

Replace type="Company\Cms\Block\Html\Topmenu" with type="Takeuchi\Cms\Block\Html\Topmenu"

  • i forgot to change Package & Module names in Block file. still facing same issue
    – Ramesh
    Nov 14 '17 at 13:10
  • try with <?xml version="1.0" encoding="UTF-8"?> encoding in your xml line Nov 14 '17 at 13:15
  • @rakesh...I tried and facing same issue and Is it mandatory create layout and template as per this link magento.stackexchange.com/questions/134093/…
    – Ramesh
    Nov 14 '17 at 13:25
  • you didnt create any module for above? if not without creating module its not possible to override core file Nov 14 '17 at 13:40
  • and i tried this blog also without changing names space & module names. catgento.com/2017/03/20/…
    – Ramesh
    Nov 14 '17 at 13:46

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.