wesleywmd wesleywmd - 3 months ago 23
PHP Question

How do I remove a MassAction from the Product Grid using Observers in Magento

I want to remove the "Delete" mass action from the Products grid. I am trying to do this from my observer. Here is my observer class

class NameSpace_Module_Model_Observer {

public function isAddProductDeletePermitted($observer) {
$block = $observer->getBlock();
if( $block !== null ) {
if( $block instanceof Mage_Adminhtml_Block_Cms_Block_Grid) {
$block->getMassactionBlock()->removeItem('delete');
// echo "DELETE";
}
}
return $this;
}


And here is my config.xml

<?xml version="1.0"?>
<config>

. . .

<global>

. . .

<events>
<adminhtml_block_html_before>
<observers>
<is_add_product_form_permitted>
<class>Dotlocal_AdvProductGrid_Model_Observer</class>
<method>isAddProductDeletePermitted</method>
</is_add_product_form_permitted>
</observers>
</adminhtml_block_html_before>
</events>
</global>
</config>


I cannot seem to get this to work. I am pretty sure this doesn't work because I am trying to removing it before it is added. Then what event should I be observing?

Answer

As said in my comment, the type you are checking is wrong.

Two possible solutions.

You get the right grid, which is Mage_Adminhtml_Block_Catalog_Product_Grid and your code become

class Some_Module_Model_Observer
{
    public function isAddProductDeletePermitted ( $observer )
    {
        $block = $observer->getBlock ();
        if ( $block !== null ) {
            if ( $block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid ) {
                $block->getMassactionBlock()->removeItem('delete');
                // echo "DELETE";
            }
        }
        return $this;
    }

}

Or you can also do it on every admin grid (with the generic block Mage_Adminhtml_Block_Widget_Grid) and then verify, with the handle of the page, if you are on the product listing page or not.

Like this :

class Some_Module_Model_Observer
{
    public function isAddProductDeletePermitted ( $observer )
    {
        $block = $observer->getBlock ();
        $layout_handle = Mage::app()->getLayout()->getUpdate()->getHandles();
        if ( $block !== null && $layout_handle[3] == 'adminhtml_catalog_product_index') {
            if ( $block instanceof Mage_Adminhtml_Block_Widget_Grid ) {
                $block->getMassactionBlock()->removeItem('delete');
                // echo "DELETE";
            }
        }
        return $this;
    }

}
Comments