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 Commented Nov 14, 2017 at 13:10

1 Answer 1


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
    Commented Nov 14, 2017 at 13:10
  • try with <?xml version="1.0" encoding="UTF-8"?> encoding in your xml line Commented Nov 14, 2017 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
    Commented Nov 14, 2017 at 13:25
  • you didnt create any module for above? if not without creating module its not possible to override core file Commented Nov 14, 2017 at 13:40
  • and i tried this blog also without changing names space & module names. catgento.com/2017/03/20/…
    – Ramesh
    Commented Nov 14, 2017 at 13:46

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.