Built-in Mass Actions
Delete Mass Action
<massactions name="listing_massactions">
<action name="delete">
<settings>
<url path="*/massDelete"/>
<confirm>
<message translate="true">Are you sure you want to delete selected items?</message>
<title translate="true">Delete Items</title>
</confirm>
</settings>
</action>
</massactions>
Status Update Mass Action
<massactions name="listing_massactions">
<action name="status">
<settings>
<url path="*/massStatus"/>
<confirm>
<message translate="true">Are you sure you want to change status?</message>
<title translate="true">Change Status</title>
</confirm>
</settings>
<argument name="data" xsi:type="array">
<item name="options" xsi:type="array">
<item name="1" xsi:type="array">
<item name="value" xsi:type="string">1</item>
<item name="label" xsi:type="string">Enabled</item>
</item>
<item name="0" xsi:type="array">
<item name="value" xsi:type="string">0</item>
<item name="label" xsi:type="string">Disabled</item>
</item>
</item>
</argument>
</action>
</massactions>
Custom Mass Actions
Custom Mass Action XML
<massactions name="listing_massactions">
<action name="custom_action">
<settings>
<url path="*/massCustomAction"/>
<confirm>
<message translate="true">Are you sure you want to perform this action?</message>
<title translate="true">Custom Action</title>
</confirm>
</settings>
</action>
</massactions>
Custom Mass Action Controller
namespace Vendor\Module\Controller\Adminhtml\Item\Mass;
class MassCustomAction extends \Magento\Backend\App\Action
{
protected function _isAllowed(): bool
{
return $this->_authorization->isAllowed('Vendor_Module::items_edit');
}
public function execute()
{
$selected = $this->getRequest()->getParam('selected');
if (empty($selected)) {
$this->messageManager->addErrorMessage(__('No items selected.'));
return $this->_redirect('*/*/index');
}
$successCount = 0;
$errorCount = 0;
foreach ($selected as $itemId) {
try {
$item = $this->itemFactory->create();
$item->load($itemId);
if (!$item->getId()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Item not found.')
);
}
// Perform custom action
$this->performCustomAction($item);
$successCount++;
} catch (\Exception $e) {
$errorCount++;
$this->messageManager->addErrorMessage(
__('Item %1: %2', $itemId, $e->getMessage())
);
}
}
if ($successCount > 0) {
$this->messageManager->addSuccessMessage(
__('%1 items processed successfully.', $successCount)
);
}
return $this->_redirect('*/*/index');
}
private function performCustomAction(
\Magento\Framework\DataObject $item
): void {
// Custom logic here
$item->setData('custom_field', 'updated_value');
$item->save();
}
}
Confirmation Dialogs
Confirmation Configuration
<!-- Simple confirmation -->
<action name="delete">
<settings>
<url path="*/massDelete"/>
<confirm>
<message translate="true">Are you sure you want to delete?</message>
<title translate="true">Confirm Delete</title>
</confirm>
</settings>
</action>
<!-- No confirmation -->
<action name="quick_action">
<settings>
<url path="*/massQuickAction"/>
<!-- No confirm element = no confirmation -->
</settings>
</action>
Custom Confirmation Message
<!-- Dynamic confirmation with item count -->
<action name="delete">
<settings>
<url path="*/massDelete"/>
<confirm>
<message translate="true">Delete %1 selected items?</message>
<title translate="true">Confirm Deletion</title>
</confirm>
</settings>
</action>
Confirmation in JavaScript
// Programmatic confirmation
require(['Magento_Ui/js/modal/confirm'], function (confirm) {
confirm({
content: 'Are you sure you want to proceed?',
title: 'Confirm Action',
actions: {
confirm: function () {
// User confirmed
performAction();
},
cancel: function () {
// User cancelled
}
}
});
});
Mass Action Processing
Mass Action with Options
<!-- Mass action with dropdown options -->
<action name="update_status">
<settings>
<url path="*/massUpdateStatus"/>
<confirm>
<message translate="true">Update status for selected items?</message>
<title translate="true">Update Status</title>
</confirm>
</settings>
<argument name="data" xsi:type="array">
<item name="options" xsi:type="array">
<item name="processing" xsi:type="array">
<item name="value" xsi:type="string">processing</item>
<item name="label" xsi:type="string">Processing</item>
</item>
<item name="complete" xsi:type="array">
<item name="value" xsi:type="string">complete</item>
<item name="label" xsi:type="string">Complete</item>
</item>
<item name="canceled" xsi:type="array">
<item name="value" xsi:type="string">canceled</item>
<item name="label" xsi:type="string">Canceled</item>
</item>
</item>
</argument>
</action>
Mass Action Controller with Option
namespace Vendor\Module\Controller\Adminhtml\Item\Mass;
class MassUpdateStatus extends \Magento\Backend\App\Action
{
public function execute()
{
$selected = $this->getRequest()->getParam('selected');
$status = $this->getRequest()->getParam('mass_status');
if (empty($selected) || empty($status)) {
$this->messageManager->addErrorMessage(__('Invalid parameters.'));
return $this->_redirect('*/*/index');
}
$updated = 0;
foreach ($selected as $itemId) {
try {
$item = $this->itemFactory->create()->load($itemId);
$item->setStatus($status);
$item->save();
$updated++;
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$this->messageManager->addSuccessMessage(
__('%1 items updated to %2 status.', $updated, $status)
);
return $this->_redirect('*/*/index');
}
}
Quiz
1. How do you add a confirmation dialog to a mass action?
2. Where do mass action controllers receive selected items?
3. How do mass actions with options work?
Flashcards
Question
Mass action XML element?
Click to reveal answer
Answer
<action name="action_name"> inside <massactions>
Question
Confirmation dialog?
Click to reveal answer
Answer
<confirm><message> and <title> inside action settings
Question
Selected items parameter?
Click to reveal answer
Answer
POST parameter 'selected' with array of IDs
Question
Mass action with options?
Click to reveal answer
Answer
Define options in argument, receive as mass_actionname
Revision Notes
Key Takeaways
- 1. Mass actions are defined in <massactions> element of listing XML
- 2. Confirmation dialogs use <confirm> with message and title
- 3. Selected items come in POST 'selected' parameter
- 4. Options for mass actions are passed via XML arguments
- 5. Controllers should handle errors and report success/failure counts
Interview Tips
- • Explain the complete mass action flow from XML to controller
- • Describe how to add a new mass action with confirmation
- • Discuss error handling and user feedback in mass actions
Cheat Sheet
Mass Actions:
<massactions> → <action name="name">
<url> → controller path
<confirm> → confirmation dialog
<options> → dropdown choices
Controller:
$this->getRequest()->getParam('selected')
$this->getRequest()->getParam('mass_actionname')
Pattern:
Loop selected → try/catch → count success/errors
redirect to index