0

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

Block file:

<?php

namespace Company\Cms\Block\Html;

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

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

and I followed the below link for this

How to override blocks in v2.1

Ramesh
  • 986
  • 1
  • 14
  • 38

1 Answers1

0

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

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

Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
  • i forgot to change Package & Module names in Block file. still facing same issue – Ramesh Nov 14 '17 at 13:10
  • try with encoding in your xml line – Rakesh Jesadiya 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 https://magento.stackexchange.com/questions/134093/how-to-override-blocks-in-v2-1/134106 – 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 – Rakesh Jesadiya Nov 14 '17 at 13:40
  • and i tried this blog also without changing names space & module names. http://catgento.com/2017/03/20/magento-2-display-category-image-in-navigation-menu/ – Ramesh Nov 14 '17 at 13:46
  • and i tried this blog also without changing names space & module names. – Ramesh Nov 14 '17 at 13:47