Hi everyone, today we start a brand new type of article, where we explain and compare development processes between Magento 1 and Magento 2.

We will try to cover as many different aspects as possible and we are going to start with the following: add a mass action item to an admin grid.

Magento 1

In Magento 1, there is several ways of doing it:

  • You can either rewrite the entire grid block and override the _prepareMassaction method to add your extra item. This is not really recommended as we would rewrite a block and possibly cause conflicts.
  • Another way of doing it is to use event observers and that is this method that we are going to overview today.

Magento 1 registers an event when an admin grid block prepares a mass action block. Let's say we want to add a massaction item to the catalog product grid. The corresponding event is adminhtml_catalog_product_grid_prepare_massaction. So the first step is to declare an observer in your module on that specific event:


    
        
            0.0.1
        
    
    
        
            
                My_Module_Model
            
        
    
    
        
            
            
                
                    
                        singleton
                        module/observer
                        addAnotherItem
                    
                
            
        
    

In this simple config.xml file, we have told Magento that, everytime a massaction block is prepared, we call the method addAnotherItem of our observer.

Now let's take a look at our observer

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

        $block->getMassactionBlock()->addItem('anotheritem', array(
            'label' => 'Another item',
            'url' => $block->getUrl('adminhtml/controller/action')
        ));
    }
}

In our addAnotherItem function, we first retrieve the block of the event (which is Mage_Adminhtml_Block_Catalog_Product_Grid in our case). Then we get the massaction block from it and we add another item using the same method as the native Magento grid block.

The addItem function requires two parameters, the first one is the item id and the second one is the item array where the item informations are defined. Here you can see that we specify a label (that will be display in the massaction dropdown) as well as the URL that will be called when we choose this new massaction item.

Please note that if you want to call a custom admin controller, you will have to define it under config.xml and create it.

Magento 2

Even if Magento 2 still registers the adminhtml_catalog_product_grid_prepare_massaction event, it is way easier than Magento 1 to add a mass action item to a grid.

Magento 2 defines the massaction items via a ui component xml file.

Thus foreach admin grid you have a corresponding ui component file:

  • Catalog product grid: app\code\Magento\Catalog\view\adminhtml\ui_component\product_listing.xml
  • Sales credit memo grid: app\code\Magento\Sales\view\adminhtml\ui_component\sales_order_creditmemo_grid.xml
  • Sales order grid: app\code\Magento\Sales\view\adminhtml\ui_component\sales_order_grid.xml
  • Sales invoice grid: app\code\Magento\Sales\view\adminhtml\ui_component\sales_invoice_grid.xml
  • And so on...

To use the same example as before, we still want to add a massaction item to our catalog product grid. So in our module folder, we need to create the following file: view/adminhtml/ui_component/product_listing.xml

    
        
            
                
                    
                        anotheritem
                        Another item
                        
                    
                
            
        
    

So first the container tag corresponds to the top part of the gridusing the listing_top name

Then the massaction tag matches the mass action block. You can find these two tags under the original product_listing.xml file.

The action tag is our new action item, we need to give it a name (same as the item id in Magento 1).

The argument tag is our argument array declaration as you can see from the xsi:type attribute.

Once we've done this, we need add our item config as an array as well. In this item array tag, we add three item tags:

  • type: which matches the action tag name
  • label: which is the text displayed in the massaction dropdown
  • url: which is the URL you will be redirected to when choosing this new item

Once again, redirecting to a custom admin URL implies you will have declared and created the admin routes but that's something that we will cover in another post

We hope you enjoyed it and feel free to comment if you have some questions or remarks