2

I am trying to rewrite Grid in Magento Admin for Newsletter Subscribers. I am using code below to rewrite Grid in Custom module:

config.xml (only required part is added)

<global>    
    <blocks>
        <custommodule>
            <class>Namespace_Module_Block</class>
        </custommodule>
        <adminhtml>
            <rewrite>
                <!-- Extra column in newsletter subscribers grid  -->
                <newsletter_subscriber_grid>Namespace_Module_Block_Adminhtml_Newsletter_Subscriber_Grid</newsletter_subscriber_grid>
            </rewrite>
        </adminhtml>
        <catalog>
            <rewrite>
                <product_list_toolbar>Namespace_Module_Block_Product_List_Toolbar</product_list_toolbar>
                <product_view>Namespace_Module_Block_Product_View</product_view>
            </rewrite>
        </catalog>
    </blocks>
<global>

Namespace_Module_Block_Adminhtml_Newsletter_Subscriber_Grid

    <?php
    class Namespace_Module_Block_Adminhtml_Newsletter_Subscriber_Grid extends Mage_Adminhtml_Block_Newsletter_Subscriber_Grid 
    {
        protected function _prepareColumns()
        {
            // extra grid column added
        }
    }

But Grid is not updating in Admin... Anything wrong I am doing here...??

2

1 Answer 1

5

First, your configuration file should be config.xml and not config.php.

Second it must declare the module like this, this bit seems to be missing in the code you pasted:

<modules>
    <Namespace_Module>
        <version>0.0.1</version>
    </Namespace_Module>
</modules>

And the XML code must be surrounded with a <config> tag.

Now, most of the time, the main error when trying to add columns to an existing grid using block rewrite is that the extended class method calls the following:

return parent::_prepareColumns();

Calling this will call the original _prepareColumns method and thus, override the extra column you're trying to add.

To avoid that, you need to call the following instead in your custom code:

return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();

Personal Suggestion

Instead of rewriting the entire block, use an observer.

In your config.xml add the following code under the <config> tag:

<adminhtml>
    <events>
        <core_layout_block_create_after>
            <observers>
                <namespace_module_after_block>
                    <type>singleton</type>
                    <class>Namespace_Module_Model_Observer</class>
                    <method>blockCreateAfter</method>
                </namespace_module_after_block>
            </observers>
        </core_layout_block_create_after>
    </events>
</adminhtml>

Now create the following file Namespace/Module/Model/Observer.php with the following content:

<?php

class Namespace_Module_Model_Observer {
    public function blockCreateAfter(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();

        if ($block instanceof Mage_Adminhtml_Block_Newsletter_Subscriber_Grid) {
            $block->addColumnAfter('customcolumn',
                array(
                    'header' => Mage::helper('newsletter')->__('Custom Column'),
                    'index' => 'customcolumn'
                ),
                'after_column'
            );
        }
    }
}

In this code you need to replace the after_column with the name of the original column you want to place your custom column after.

0

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.