Magento 1.9 Module – Overriding Custom Module Block

magento-1.9module

I would like to override a specific block of a module that I've downloaded, because there is a specific methods I would like to add, and I don't like the idea of modifying module's core files.

I have hard time to structure my xml correctly, which is why my block does not gets loaded instead of the original one. I've tried following the tips provided by @Joseph in this question, but no luck.

Now, let me provide some file structure.

The interesting part of the downloaded module config.xml

<?xml version="1.0"?>
    <config>
        <modules>
            <EM_Megamenupro>
                <version>1.0.0</version>
            </EM_Megamenupro>
        </modules>
        <blocks>
            <megamenupro>
                <class>EM_Megamenupro_Block</class>
            </megamenupro>
        </blocks>
    </config>

The block I am trying to modify is located in app/local/EM/Megamenupro/Block/Megamenupro.php

Now, my module is located in app/local/Acme/Demo, my block is located in app/local/Acme/Demo/Block/Megamenu.php

My module is active(I checked it up in Administrative panel, its there)

In my config.xml file I've added the following nodes, but it does not appear to be working.

<global>
    <blocks>
        <megamenupro>
            <rewrite>
                <megamenu>Acme_Demo_Block_Megamenu</megamenu>
            </rewrite>
        </megamenupro>
    </blocks>
</global>

Any kind of help would be kindly appreciated. Thank you.

Best Answer

Your config.xml should look like this:

<global>
    <blocks>
        <megamenupro>
            <rewrite>
                <megamenupro>Acme_Demo_Block_Megamenu</megamenupro>
            </rewrite>
        </megamenupro>
    </blocks>
</global>

As a general rule:
The config in the original extension states this:

<blocks>
    <megamenupro>
        <class>EM_Megamenupro_Block</class>
    </megamenupro>
</blocks>

You need to rewrite EM/Megamenupro/Block/Megamenupro.php that is referenced internaly as megamenupro/megamenupro.
Your config should look like this

<global>
    <blocks><!-- you are rewriting a block -->
        <megamenupro><!-- the same tag as in the original extension under the <blocks> tag -->
            <rewrite> <!-- this is fixed -->
                <megamenupro>Acme_Demo_Block_Megamenu</megamenupro><-- this tag should be what's after the `Block/` part in the class you want to rewrite. so  -EM/Megamenupro/Block/Megamenupro.php becomes 'megamenupro'->
            </rewrite>
        </megamenupro>
    </blocks>
</global>