How to Add Dynamic Mass Action in Admin Grid in Magento 2

The Mass Actions component allows performing specific actions with multiple selected items. Admin can perform a mass operation on the selected items quickly. It saves the admin from the tedious process of completing an operation on a one-by-one record. 

Having a grid in Magento 2 means loads of records maintained in that admin grid, and need often arrives to perform mass action on the various records. No individual or separate record operation is required if the admin grid has a mass action facility. In addition, the admin grid provides the flexibility to remove columns from admin grid dynamically from the table, allowing for a more tailored and efficient view of the data.

Magento 2 comes with a UI component that assists store admins in adding more items such as columns, filter argument, paging, mass action, etc., for the admin grid.

Earlier, I did post a solution to add custom mass action in order grid in Magento 2.

However, sometimes you may have to dynamically add mass action in the admin grid according to your requirements. For example you can dynamically add link to customer account navigation in Magento 2 allows you to customize and enhance the customer’s account dashboard by adding custom links or sections based on specific business needs and requirements.

For instance, the admin grid has a column named ‘status’ with three records- pending, confirmed, and unconfirmed. Now admin wants to change all the unconfirmed status with confirmed one and pending status with confirmed. Also, if a new status “cancelled” is created, the cancel status must be available as an option dynamically while using this custom mass action.

Or, if you are using the custom mass action to upgrade the customer group of your selected customers from the grid and you create a custom customer group, using the below solution to add dynamic mass action in admin grid in Magento 2, your custom group will be available as an option dynamically! 

Method to Add Dynamic Mass Action in Admin Grid in Magento 2

1. Use the below code in the data_listing.xml at Vendor/Extension/view/adminhtml/ui_component

<massaction name="listing_massaction">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
            <item name="indexField" xsi:type="string">id</item>
        </item>
    </argument>
    <action name="change_status">
        <settings>
            <type>change_status</type>
            <label translate="true">Change Status</label>
            <actions class="Vendore\Extension\Ui\Component\MassAction\Status\Options"/>
        </settings>
    </action>
</massaction>

2. Use the below code in the di.xml file at Vendor/Extension/etc

<type name="Vendore\Extension\Ui\Component\MassAction\Status\Options">
    <arguments>
        <argument name="data" xsi:type="array">
            <item name="urlPath" xsi:type="string">extenssion/index/masschangestatus</item>
            <item name="paramName" xsi:type="string">status</item>
            <item name="confirm" xsi:type="array">
                <item name="title" xsi:type="string" translatable="true">Change Status</item>
                <item name="message" xsi:type="string" translatable="true">Are you sure to change?</item>
            </item>
        </argument>
    </arguments>
</type>

3. Paste the below code in the Options.php file at Vendor/Extension/UI/Component/MassAction/Status

<?php

namespace Vendore\Extension\Ui\Component\MassAction\Status;

use Magento\Framework\Phrase;
use Magento\Framework\UrlInterface;

class Options implements \JsonSerializable
{
    protected $options;
    protected $data;
    protected $urlBuilder;
    protected $urlPath;
    protected $paramName;
    protected $additionalData = [];

    public function __construct(
        UrlInterface $urlBuilder,
        array $data = []
    )
    {
        $this->data = $data;
        $this->urlBuilder = $urlBuilder;
    }

    public function jsonSerialize()
    {
        if ($this->options === null) {
            $options = [[
                'value' => 'pending',
                'label' => 'Pending approval'
            ], [
                'value' => 'active',
                'label' => 'Active'
            ], [
                'value' => 'inactive',
                'label' => 'Inactive'
            ]];
            $this->prepareData();
            foreach ($options as $optionCode) {
                $this->options[$optionCode['value']] = [
                    'type' => 'change_status_' . $optionCode['value'],
                    'label' => __($optionCode['label']),
                    '__disableTmpl' => true
                ];

                if ($this->urlPath && $this->paramName) {
                    $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
                        $this->urlPath,
                        [$this->paramName => $optionCode['value']]
                    );
                }

                $this->options[$optionCode['value']] = array_merge_recursive(
                    $this->options[$optionCode['value']],
                    $this->additionalData
                );
            }

            $this->options = array_values($this->options);
        }

        return $this->options;
    }

    protected function prepareData()
    {
        foreach ($this->data as $key => $value) {
            switch ($key) {
                case 'urlPath':
                    $this->urlPath = $value;
                    break;
                case 'paramName':
                    $this->paramName = $value;
                    break;
                case 'confirm':
                    foreach ($value as $messageName => $message) {
                        $this->additionalData[$key][$messageName] = (string)new Phrase($message);
                    }
                    break;
                default:
                    $this->additionalData[$key] = $value;
                    break;
            }
        }
    }
}

4. Use the below code in MassChangeStatus.php file at Vendor/Extension/Controller/Adminhtml/Index

<?php

namespace Vendore\Extension\Controller\Adminhtml\Index;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Ui\Component\MassAction\Filter;
use Vendore\Extension\Model\ExtensionModelFactory;
use Vendore\Extension\Model\ResourceModel\ExtensionModel\CollectionFactory;

;

class MassChangeStatus extends Action
{
    protected $filter;
    protected $resultPageFactory;
    protected $collectionFactory;
    protected $extensionModelFactory;
    private $scopeConfig;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        Filter $filter,
        ScopeConfigInterface $scopeConfig,
        ExtensionModelFactory $extensionModelFactory,
        CollectionFactory $collectionFactory
    )
    {
        parent::__construct($context, $resultPageFactory);
        $this->resultPageFactory = $resultPageFactory;
        $this->filter = $filter;
        $this->scopeConfig = $scopeConfig;
        $this->extensionModelFactory = $extensionModelFactory;
        $this->collectionFactory = $collectionFactory;
    }

    public function execute()
    {
        try {
            $collection = $this->filter->getCollection($this->collectionFactory->create());
            $updated = 0;
            foreach ($collection as $item) {
                $model = $this->extensionModelFactory->create()->load($item['id']);
                $model->setData('status', $this->getRequest()->getParam('status'));
                $model->save();
                $updated++;
            }
            if ($updated) {
                $this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $updated));
            }

        } catch (\Exception $e) {
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
        }
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }

    protected function _isAllowed()
    {
        return true;
    }
}

After applying the above method, the mass action displays in the admin grid as shown below:

How to Add Dynamic Mass Action in Admin Grid in Magento 2

Quite lengthy but worth applying. Right?

Do consider sharing this post with Magento Community via social media.

Thank you.

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...