Posted on

How to add a mass action via xml in Magento 2?

Scenario:
I have a custom CRUD module and I need to have a mass action in the admin grid that I can use to mass update the status for example.
The module uses the new xml grid system from Magento 2 (see as example app/code/Magento/Sales/view/adminhtml/layout/sales_order_grid_block.xml).

Issue:
I can add any mass action item that does not require an additional input.

<block class=”MagentoBackendBlockWidgetGridMassaction” name=”[module].[entity].grid.massaction” as=”grid.massaction”>
<arguments>
<argument name=”massaction_id_field” xsi:type=”string”>[entity]_id</argument>
<argument name=”form_field_name” xsi:type=”string”>[entity]_ids</argument>
<argument name=”use_select_all” xsi:type=”string”>1</argument>
<argument name=”options” xsi:type=”array”>
<item name=”delete” xsi:type=”array”>
<item name=”label” xsi:type=”string” translate=”true”>Delete</item>
<item name=”url” xsi:type=”string”>[module_route]/[entity]/massDelete</item>
<item name=”confirm” xsi:type=”string” translate=”true”>Are you sure you want to delete the items?</item>
</item>
</argument>
</arguments>
</block>

I cannot add a mass action item that requires an additional select. In the example above, I need a select with 2 statuses enabled & disabled.

In Magento 1 this was possible via _prepareMassaction method in the grid block:

$this->getMassactionBlock()->addItem(
‘status’,
array(
‘label’ => Mage::helper(‘[module]’)->__(‘Change status’),
‘url’ => $this->getUrl(‘*/*/massStatus’, array(‘_current’=>true)),
‘additional’ => array( //this allows an additional select.
‘status’ => array(
‘name’ => ‘status’,
‘type’ => ‘select’,
‘class’ => ‘required-entry’,
‘label’ => Mage::helper(‘[module]’)->__(‘Status’),
‘values’ => array(
‘1’ => Mage::helper(‘[module]’)->__(‘Enabled’),
‘0’ => Mage::helper(‘[module]’)->__(‘Disabled’),
)
)
)
)
);

Magento 1 result:

Question:
How can I get the same result in Magento 2 (0.74.0-beta1) via xml grid definition?

Leave a Reply

Your email address will not be published.