Following our new release of the module DigitalPianism ExportReview, we are going to describe how this module works and in general, explain how to use Magento event observer to add mass action items to a grid without extending Magento blocks.

This example is made on the product reviews grid but can be easily adapted to any other grid in Magento backend.

The code

Let's focus on the app/code/community/DigitalPianism/ExportReview/etc/config.xml file first:


 
    
		
			0.1.0
		
    
	
	
		
			
				DigitalPianism_ExportReview_Model
			
		
	
 
    
        
            
				admin
                
                    DigitalPianism_ExportReview
					exportreview
                
            
        
    

	
		
			
			
				
					
						singleton
						exportreview/observer
						addMassExport
					
				
			
		
	

As you may already know the first part of the module inside the tags is needed to declare our models.

Then, the node is used to declare a new controller route.In this example, exportreview is the new route created.

Finally, the interesting part is in the tag. The core_block_abstract_prepare_layout_before is an event dispatched under app/code/core/Mage/Core/Block/Abstract.php file. It is dispatched in the setLayout function right before the layout preparation.

We are using this event to call the function addMassExport in our observer located in app/code/community/DigitalPianism/ExportReview/Model/Observer.php.

Please note that the observer tag is defined by the developer and every observer tag always require two parameters: class and method.

If we check the Observer.php file here is what we can see:

class DigitalPianism_ExportReview_Model_Observer
{
    public function addMassExport(Varien_Event_Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();

        if($block instanceof Mage_Adminhtml_Block_Widget_Grid_Massaction
            && $block->getRequest()->getControllerName() == 'catalog_product_review')
        {
            $block->addItem('exportreviewcsv', array(
                'label' => 'Export to CSV',
                'url' => $block->getUrl('exportreview/adminhtml_index/massCsvExport')
            ));
        }
    }
}

Our observer function takes one parameter which is a Varien_Event_Observer. We can retrieve the Magento block by calling getEvent() then getBlock on this parameter to start working with it.

Once we have retrieved this block and as the core_block_abstract_prepare_layout_before event is called for every block, we need to ensure we are going to alter the right block before going on.

So we first check if the block is an instance of the Mage_Adminhtml_Block_Widget_Grid_Massaction class (which is the block used for the massaction part of the grid) then we check that we are on the right admin section using getRequest() then getControllerName on our block instance.

After that we can use the addItem function to add our custom mass action. This function takes two parameter, the first one is the ID of the mass action item and the second one is an array containing a label (that will be displayed in the mass action dropdown) and an URL (that will be called once we submit our mass action).

As you can see our URL here is related to the custom route we declared earlier in the config.xml file.

Finally, this route will lead us to app/code/community/DigitalPianism/ExportReview/controllers/Adminhtml/IndexController.php where all the magic happens:

class DigitalPianism_ExportReview_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
{
    public function massCsvExportAction()
    {
        $reviewIds = $this->getRequest()->getParam('reviews');
        // Do stuff with our reviews
    }
}

And voila, that's all you need to do.

Useful to know

In order to check what parameters is being sent to your controller in case you are working on a different grid, you can use:

Mage::log($this->getRequest()->getParams());

Another good resource is the Alan Storm CommerceBug that can be used to find out which event is being dispatched on a specific page and can help you choosing the right event for the development.