1

Sorry for the noob question, but I just can't get my head around Magento's structural concepts.

I have the following class:

class AW_Autorelated_Block_Widget_Block extends AW_Autorelated_Block_Blocks implements Mage_Widget_Block_Interface
{
    public function getBlocksHtml()
    {
        ...extension default code here...
    }
}

It's located in the following directory:

app/code/local/AW/Autorelated/Block/Widget/Block.php

I would like to override that getBlocksHtml() method.

I've tried creating the following config.xml but it doesn't work:

<?xml version="1.0"?>
<config>
    <global>
        <blocks>
            <awautorelated>
                <rewrite>
                    <awautorelated>My_Autorelated_Block_Blocks</awautorelated>
                </rewrite>
            </awautorelated>
        </blocks>
    </global>
</config>

Here is the original module's config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <AW_Autorelated>
            <version>2.4.12</version><platform>ce</platform>
        </AW_Autorelated>
    </modules>
    <frontend>
        <layout>
            <updates>
                <awautorelated module="AW_Autorelated">
                    <file>aw_autorelated.xml</file>
                </awautorelated>
            </updates>
        </layout>
        <translate>
            <modules>
                <AW_Autorelated>
                    <files>
                        <default>AW_Autorelated.csv</default>
                    </files>
                </AW_Autorelated>
            </modules>
        </translate>
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <awauterelated_rc>
                        <class>awautorelated/observer</class>
                        <method>replaceCrossselsBlock</method>
                    </awauterelated_rc>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
    </frontend>
    <global>
        <helpers>
            <awautorelated>
                <class>AW_Autorelated_Helper</class>
            </awautorelated>
        </helpers>
        <models>
            <awautorelated>
                <class>AW_Autorelated_Model</class>
                <resourceModel>awautorelated_mysql4</resourceModel>
            </awautorelated>
            <awautorelated_mysql4>
                <class>AW_Autorelated_Model_Mysql4</class>
                <entities>
                    <blocks>
                        <table>aw_arp_blocks</table>
                    </blocks>
                    <rule>
                        <table>aw_arp_rule</table>
                    </rule>
                </entities>
            </awautorelated_mysql4>
            <salesrule>
                <rewrite>
                    <rule_condition_product_found>AW_Autorelated_Model_Salesrule_Rule_Condition_Product_Found</rule_condition_product_found>
                </rewrite>
                <rewrite>
                    <rule_condition_product_subselect>AW_Autorelated_Model_Salesrule_Rule_Condition_Product_Subselect</rule_condition_product_subselect>
                </rewrite>
            </salesrule>
        </models>
        <resources>
            <awautorelated_setup>
                <setup>
                    <module>AW_Autorelated</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </awautorelated_setup>
            <awautorelated_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </awautorelated_write>
            <awautorelated_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </awautorelated_read>
        </resources>
        <blocks>
            <awautorelated>
                <class>AW_Autorelated_Block</class>
            </awautorelated>
        </blocks>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <awautorelated after="Mage_Adminhtml">AW_Autorelated_Adminhtml</awautorelated>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <menu>
            <catalog>
                <children>
                    <awautorelated module="awautorelated">
                        <title>Automatic Related Products</title>
                        <sort_order>10</sort_order>
                        <children>
                            <new module="awautorelated">
                                <title>Add Block</title>
                                <sort_order>10</sort_order>
                                <action>adminhtml/awautorelated_blocksgrid/new</action>
                            </new>
                            <manage module="awautorelated">
                                <title>Manage Blocks</title>
                                <sort_order>20</sort_order>
                                <action>adminhtml/awautorelated_blocksgrid/list</action>
                            </manage>
                            <settings module="awautorelated">
                                <title>Configuration</title>
                                <sort_order>30</sort_order>
                                <action>adminhtml/system_config/edit/section/awautorelated</action>
                            </settings>
                        </children>
                    </awautorelated>
                </children>
            </catalog>
        </menu>
        <enterprise>
            <admingws>
                <collection_load_before>
                    <awautorelated__blocks_collection>addStoreFilter</awautorelated__blocks_collection>
                </collection_load_before>
            </admingws>
        </enterprise>
        <acl>
            <resources>
                <all>
                    <title>Allow everything</title>
                </all>
                <admin>
                    <children>
                        <catalog>
                            <children>
                                <awautorelated module="awautorelated">
                                    <title>Automatic Related Products</title>
                                    <sort_order>10</sort_order>
                                    <children>
                                        <new module="awautorelated">
                                            <title>Manage Blocks</title>
                                            <sort_order>10</sort_order>
                                        </new>
                                        <manage module="awautorelated">
                                            <title>View Blocks</title>
                                            <sort_order>20</sort_order>
                                        </manage>
                                        <settings module="awautorelated">
                                            <title>Settings Quick Link</title>
                                            <sort_order>30</sort_order>
                                        </settings>
                                    </children>
                                </awautorelated>
                            </children>
                        </catalog>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <awautorelated>
                                            <title>Automatic Related Products section</title>
                                        </awautorelated>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
        <layout>
            <updates>
                <awautorelated module="awautorelated">
                    <file>aw_autorelated.xml</file>
                </awautorelated>
            </updates>
        </layout>
        <translate>
            <modules>
                <AW_Autorelated>
                    <files>
                        <default>AW_Autorelated.csv</default>
                    </files>
                </AW_Autorelated>
            </modules>
        </translate>
    </adminhtml>
    <default>
        <awautorelated>
            <general>
                <replacenative>0</replacenative>
                <pcount>10</pcount>
            </general>
        </awautorelated>
    </default>
</config>
3
  • see this question and my answer Best way create self block for your module, because if you overide this block, and want disable you block you needed remove call custom methods for work, if you don't rewrite and create self block you just disable you extensions and get work application. p.s. sorry link to question magento.stackexchange.com/questions/124471/…
    – Naumov
    Commented Jul 11, 2016 at 16:46
  • Please post the original module config.xml file especially the block declaration part under the global tag Commented Jul 11, 2016 at 17:27
  • @RaphaelatDigitalPianism As per requested, I posted the original config.xml. Thanks.
    – Nathan
    Commented Jul 11, 2016 at 17:38

1 Answer 1

0

Magento 1's class rewrite system works because every model, helper, and block objects is instantiated using a factory method of some sort, and that factory method accepts a class alias string.

In other words, when Magento creates a block, it does so with code that looks like the following

$block = Mage::getSingleton('core/layout')
    ->createBlock('catalog/listing');
$block = Mage::getSingleton('core/layout')
    ->createBlock('foo/bar');

The class aliases are the string catalog/listing, and foo/bar above. A class alias is separated into two components -- the group name, and the model name.

$block = Mage::getSingleton('core/layout')->createBlock('group/model_name');          

So, step one to rewriting a block object in Magento 1 is to identify the class alias for your block. Based on the class name in your example, my guess is the block's alias is awautorelated/widget_block. You can usually test this by running the following code somewhere

$block = Mage::getSingleton('core/layout')
    ->createBlock('awautorelated/widget_block');

var_dump( get_class($block) );        

So, if the above is true, your block's group name is awautorelated, and your block's class name is widget_block.

Your next step is to create your rewrite XML. The general syntax for this is

<global>
    <blocks>
        <group_name>
            <rewrite>
                <model_name>My_Autorelated_Block_Blocks</model_name>
            </rewrite>
        </group_name>
    </blocks>
</global>

That is, you create a sub-node in block that's the class alias's group name. Under that node you create a node named rewrite. Under that node you create a node whose name is the class alias's model name. The text content of that final node is your new class name for this block.

So, looking at the XML you have

<global>
    <blocks>
        <awautorelated>
            <rewrite>
                <awautorelated>My_Autorelated_Block_Blocks</awautorelated>
            </rewrite>
        </awautorelated>
    </blocks>
</global>

You're rewriting a block whose class alias is awautorelated/awautorelated. If you wanted to rewrite the awautorelated/widget_block class alias, you'd want

<global>
    <blocks>
        <awautorelated>
            <rewrite>
                <widget_block>My_Autorelated_Block_Blocks</widget_block>
            </rewrite>
        </awautorelated>
    </blocks>
</global>    

Finally, if you're having trouble debugging your class rewrites, here's the central location where the class swapping happens.

Hope that helps, and good luck!

2
  • Thanks for the excellent write-up Alan. I was able to see the var_dump confirm that the alias was correct. But when I over-wrote it with mine, it didn't take. Here is what I got: bit.ly/29yJkib
    – Nathan
    Commented Jul 11, 2016 at 18:23
  • @Nathan Debugging the method at magento.stackexchange.com/questions/6631/… is your best bet. That's where Magento looks into the configuration for a class name. Figure out why that code can't find awautorelated/widget_block and you'll be set. Also -- of possible interest to you -- I run a patreon that providers mentoring/office hours for all levels of support. Useful if you need extended debugging help. Good luck! patreon.com/alanstorm Commented Jul 11, 2016 at 18:56

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.