Magento – How to remove the “Delete” mass action from an observer

codeevent-observergridmassactionxml

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?

Best Answer

You have an error in your Observer class because Mage_Adminhtml_Block_Cms_Block_Grid should be Mage_Adminhtml_Block_Catalog_Product_Grid

However you are also right that there is a better event to use, you can use 'adminhtml_catalog_product_grid_prepare_massaction' so you can do the same code but without the instanceof check and that will do what you need.

That said... I obviously don't know why you want to do this but I imagine if I was doing it it's because I don't want person X to accidentally remove a bunch of products. And so you could go a little futher and remove this action on a user basis by adding it as a acl configuration and checking if that user is allowed in the observer as per below.

enter image description here

etc/adminhtml.xml

<?xml version="1.0"?>
<config>
    <acl>
        <resources>
            <admin>
                <children>
                    <catalog>
                        <children>
                            <products>
                                <children>
                                    <mass_delete>
                                        <title translate="title">Mass Delete</title>
                                        <sort_order>10</sort_order>
                                    </mass_delete>
                                </children>
                            </products>
                        </children>
                    </catalog>
                </children>
            </admin>
        </resources>
    </acl>
</config>

Model/Observer.php

<?php
class NameSpace_ModuleName_Model_Observer
{
    public function isAddProductDeletePermitted($observer) {
        if (!$this->_isAllowedAction('mass_delete')) {
            $block = $observer->getBlock();
            $block->getMassactionBlock()->removeItem('delete');
        }
        return $this;
    }

    protected function _isAllowedAction($action)
    {
        return Mage::getSingleton('admin/session')->isAllowed(
            'catalog/products/' . $action
        );
    }

}
Related Topic