5

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??

1 Answer 1

11

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 :)

7
  • thats great, thanks! It did add the mass action selector on top of grid, but I can't add column that would be 'binded' the items counting..? You got any more tips how to go with that.. Oct 6, 2013 at 12:28
  • Hi, Is there any reason you need to add the new column? If it is only to get a unique ID for the mass_action code, you don't need it. You can pass the identifier (as per my example code), which is unique per entry. You can use this then to manipulate the data.
    – ProxiBlue
    Oct 7, 2013 at 3:22
  • Your $block->addColumn as per your question, should still work here. You are still manipulating the same block, just via a different event.
    – ProxiBlue
    Oct 7, 2013 at 3:32
  • Yes, I have entered correct identifier, in my case for CMS->BLock it is 'block_id', but on magento 1.7 the column is not rendered but on magento 1.4 all cool and working great! here,, this column: inchoo.net/wp-content/uploads/2010/09/massaction_grid.png Oct 7, 2013 at 7:34
  • 1
    Ok, gotcha - I updated the answer with new code which ads in the massaction column. :)
    – ProxiBlue
    Oct 7, 2013 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.