overriding a magento block in multiple modules (and how to ignore the other ones)


Question

In Magento you can override a block (or helper, or model, etc.) from one module in another one (most common from Magento Adminhtml module). Now I have the problem that I want to override a block that was already overriden by some other extension. Is there any way to tell magento to ignore the <rewrite> tag in some other module configuration similary to the <remove> tag in layout updates?

Here is the configuration of the other extension:

    <blocks>
        <adminhtml>
            <rewrite>
                    <catalog_product_grid>Symmetrics_DeliveryTime_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
            </rewrite>
        </adminhtml>
    </blocks>

Here the configuration of my extension:

    <blocks>
        <adminhtml>
            <rewrite>
                    <catalog_product_grid>Namespace_MyModule_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
            </rewrite>
        </adminhtml>
    </blocks>

I integrated the functionality of the block from Symmetrics_DeliveryTime into my own block and now I want magento to ignore the block overriding from that module.

Is there any magento way to do that or is the only way to comment out the configuration (which is what I have done for now) including all the hassle while updating that module later?

1
7
1/29/2010 12:47:39 PM

Accepted Answer

If you set your module to 'depend' on the one that's also doing a rewrite, your config will be read last and the rewrite should stick, in the appropriate file within app/etc/modules you should have:

<config>
  <modules>
    <Your_Module>
      <active>true</active>
      <codePool>local</codePool>
      <depends>
        <Other_Module/>
      </depends>
    </Your_Module>
  <modules>
<config>
9
1/29/2010 8:22:33 PM

The best way to overcome this problem using, layout after or before observer.

app/code/local/My/Module/etc/config.xml

<config>
<adminhtml>
    <events>
        <core_block_abstract_prepare_layout_before>
            <observers>
                <mymodule>
                    <!-- Add column to catalog product grid -->
                    <class>mymodule/adminhtml_observer</class>
                    <method>onBlockHtmlBefore</method>
                </mymodule>
            </observers>
        </core_block_abstract_prepare_layout_before>
    </events>
</adminhtml>

or can use event: core_block_abstract_prepare_layout_after.

And in Observer:

class My_Module_Model_Adminhtml_Observer
 {
   public function onBlockHtmlBefore(Varien_Event_Observer $observer) {
    $block = $observer->getBlock();
    if (!isset($block)) return;

    switch ($block->getType()) {
        case 'adminhtml/catalog_product_grid':  //or here you can put any other block 

            /*For example i am using Mage_Adminhtml_Block_Catalog_Product_Grid, and want  to add a column*/

            $block->addColumn('COLUMN_ID', array(
                'header' => Mage::helper('mymodule')->__('COLUMN HEADER'),
                'index'  => 'COLUMN_ID',
            ));
             break;

            case 'adminhtml/catalog_product_edit_tabs':

             /*Or i want to add new tab to admin product edit*/
              /* @var $block Mage_Adminhtml_Block_Catalog_Product_Edit_Tabs */
            $block->addTab('tab_name', array(
                        'label'     => Mage::helper('catalog')->__('Tab Tilte'),
                        'content'   => $block->getLayout()->createBlock('modulename/adminhtml_product_edit_tabname')->toHtml(),
                        'after'     => 'inventory',
            ));
            break;
    }
  }
}

While adding tab then you can take reference of product edit tabs to add your form fields into that using you module.

Hope this will be helpful to some one :)


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon