Question

I'd like to add mass action column in CMS->Blocks only via observer, and not to overwrite blocks.. ok, so far I have this:

enter image description here

code:

I'm targeting this event:

<events>
  <core_block_abstract_prepare_layout_before>
    <observers>
      <core_block_abstract_prepare_layout_before_handler>
        <type>singleton</type>
        <class>masspages/observer</class>
        <method>addMassAction</method>
        <args></args>
      </core_block_abstract_prepare_layout_before_handler>
    </observers>
  </core_block_abstract_prepare_layout_before>
</events>

and observer code for test col is:

public function addMassAction(Varien_Event_Observer $observer)
{
    $block = $observer->getEvent()->getBlock();

    if ($block instanceof Mage_Adminhtml_Block_Cms_Block_Grid) {

        $block->addColumn('block_id', array(
            'header' => 'My title',
            'index' => 'block_id',
        ));
    }
}

}

I have tried to put function _prepareMassaction() somewhere,, but how to call it makes me puzzled??

Was it helpful?

Solution

You are on the right track :)

Firstly, use the event adminhtml_block_html_before instead of core_block_abstract_prepare_layout_before_handler

Then in your observer you can add this code to get a mass action added:

public function adminhtml_block_html_before($event) {

    $block = $event->getBlock();
    if ($block instanceof Mage_Adminhtml_Block_Cms_Block_Grid) {


        $block->setMassactionIdField('block_id');
        $block->getMassactionBlock()->setFormFieldName('block_ids');
        $block->getMassactionBlock()->setUseSelectAll(false);

        $block->getMassactionBlock()->addItem('YOUR_MASS_ACTION_ITEM_NAME', array(
            'label' => 'YOUR LABEL',
            'url' => $block->getUrl('*/*/YOUR CONTROLLERACTION'),
        ));

        $columnId = 'massaction';

        $massactionColumn = array(
            'index' => $block->getMassactionIdField(),
            'filter_index' => $block->getMassactionIdFilter(),
            'type' => 'massaction',
            'name' => $block->getMassactionBlock()->getFormFieldName(),
            'align' => 'center',
            'is_system' => true
        );

        if ($block->getNoFilterMassactionColumn()) {
            $massactionColumn->setData('filter', false);
        }

        // rearrange the columns;
        $oldColumns = $block->getColumns();
        foreach($oldColumns as $column){
           $block->removeColumn($column->getId());  
        }

        $block->addColumn($columnId, $massactionColumn);
        $block->addColumn('block_id', array(
            'header' => Mage::helper('cms')->__('ID'),
            'width' => '50px',
            'type' => 'number',
            'index' => 'block_id',
        ));

        // put back the original columns
        foreach($oldColumns as $column){
            $block->addColumn($column->getId(),$column->getData());
        }

        return $this;
    }
}

Hope this helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top