10

There is a third-party extension which rewrites a specific Magento core class. It should not matter if it is a block, helper or model, but in this case it is a model. Now I encounter that this rewrite is bad and I would like to disable it without changing the third party extension. Hence, I write a custom extension which depends on the third party extension. What is the best way to disable the third party rewrite? The safe one I can think of is to define a custom rewrite, which is used instead of the third party rewrite:

<global>
    <models>
        <custom_extension>
            <class>Custom_Extension_Model</class>
        </custom_extension>
        <mage_core>
            <rewrite>
                <sth>Custom_Extension_Model_Sth</sth>
            </rewrite>
        </mage_core>
    </models>
</global>

Sth.php, class directly extends from the Mage class and therefore disables the third party rewrite:

class Custom_Extension_Model_Sth extends Mage_Core_Model_Sth
{

}

This works, but does not feel too good. Thinking about observers, it is possible to disable them only via config.xml by setting them to disabled. Is something like this possible with rewrites? I tried to use the following and it works:

<global>
    <models>
        <mage_core>
            <rewrite>
                <sth></sth>
            </rewrite>
        </mage_core>
    </models>
</global>

But is it safe to do that? Does it work in all versions? I never saw that in the wild, that is why I am asking.

1

Aslong as the node is truely empty magento will think there was no rewrite and 'use class prefix to form class name' asif the node was never their. In Mage_Core_Model_Config::getGroupedClassName.

I do think however that that you should use <sth/> rather than <sth></sth> to stop formatting changing the tag to not being empty ('\n' in magento's eyes) which will throw end up with a class not found error.

I don't belive there is a 'disable' option for helpers, blocks or models. You could also use an Observer (wich will bring the config back how it should be exactly) but the problem I see with that is you then force any module that wants to add that rewrite back in after your module to either use an observer allso or to know to disable your observer via XML.

Something like:

    public function controllerActionPredispatch(Varien_Event_Observer $event)
    {
        $helper = Mage::helper('webtise_foundationalerts');
        if (! $helper->isAdmin()) {
            $node = Mage::getConfig()->getNode('global/models/core/rewrite');
unset($node->sth);
        }
    }

So for me the XML way wins.

3

I would say if you make a custom module to counteract this make sure it has a dependency on the module it's overwriting.

To prevent unexpected behavior tho I'd probably redefine the rewrite in a 'proper' way

<global>
    <models>
        <mage_core>
            <rewrite>
                <sth>Mage_Core_Model_Sth</sth>
            </rewrite>
        </mage_core>
    </models>
</global>

Recently came across an issue where 1 empty tag inside an other tag wasn't detected by Magento, returned null instead of an object which threw a warning. Not fatal but in developer mode and for logging quite annoying.

But no hard evidence here to prove your way would cause errors :)

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.