0

I want to get and modify the Mage_Adminhtml_Block_Sales_Order_Grid block from within an observer which is listening for the event resource_get_tablename.

In my observer I'm using the code

if($observer->getTableName() == 'sales_flat_order_grid') {
    $block = Mage::getBlockSingleton('Sales_Order_Grid');
}

Whilst this does get an instance of Sales_Order_Grid its not the one that has already been instantiated by the system.

I added some logging to the constructor of Mage_Adminhtml_Block_Sales_Order_Grid and I can see that its called twice. I hadn't expected this behaviour as the word Singleton, in the getBlockSingleton method, to me implies that there will be only one instance stored in the registry.

1 Answer 1

2

getBlockSingleton('Sales_Order_Grid') will return the same instance on every call, but it will not return a block previously instantiated by other means (i.e. not as a singleton).

There are very little valid use cases for getBlockSingleton() and you should not use it for regular blocks in the layout hierarchy. I'd even argue to never use it at all.

To refer an existing block in the layout, you have to know its name, not its class. Then you can use

Mage::getSingleton('core/layout')->getBlock($name);

In your case, the name is probably sales.order.grid. This name in the XML is for AJAX requests only, but the name for the automatically created grid block should be the same. You can also use template hints to find out block names.

1
  • Template hints didn't help get the block name, so I used $block->getNameInLayout() withing the block in question. Turns out it was called "sales_order.grid". Thank you.
    – Dom
    Commented Oct 10, 2016 at 12:18

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.