8

I need to add mass action based on Settings.

If in Configuration settings, it is set to "Yes/Enable" than only that particular mass-action should be added to product grid.

I know I can directly add mass-action using ui_component product_listing.xml. ? But case is how to check Configuration settings and add or remove based on that?

5
  • Also if not possible with ui_component - product_listing.xml, than the other way I think is to do it with event observer. but again I am not able to do it with either way with event observer also. Commented Dec 2, 2016 at 14:02
  • I tried to do it using a plugin, but unsuccessfully. I don't know how to disable the action element inside the child components of the mass-action. If my research is interesting for you I can post it as an answer later. Commented Dec 2, 2016 at 14:19
  • For event observer, please check this question magento.stackexchange.com/questions/148638/… Commented Dec 2, 2016 at 14:28
  • @SiarheyUchukhlebau Means you want to remove an item from mass-action drop-down.? Commented Dec 2, 2016 at 14:31
  • Yes, I tried to make it, but without success :( Commented Dec 3, 2016 at 10:41

2 Answers 2

2

To add a custom mass action to ui grid based on some condition you can use custom class for massaction ui component:

<!--/app/code/YourNamespace/YourModule/view/adminhtml/ui_component/product_listing.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top">
        <massaction name="listing_massaction" class="YourNamespace\YourModule\Ui\CustomMassAction"/>
    </listingToolbar>
</listing>

And implement this class extending the core component from Magento_Ui module.

namespace YourNamespace\YourModule\Ui;
use Magento\Ui\Component\MassAction;
class CustomMassAction extends MassAction
{
    public function prepare()
    {
        parent::prepare();

        if ($this->isEnabled()) {
            $config = $this->getConfiguration();
            $config['actions'][] = [
                'component' => 'uiComponent',
                'type' => 'custom',
                'label' => 'Custom',
                'url' => '//google.com'
            ];
            $this->setData('config', $config);
        }
    }

    public function isEnabled()
    {
        return true; // access your configuration here
    }
}

Alternatively you can apply vice-versa approach :) . Specify configuration for your massaction in product_listing.xml and remove your action from configuration in custom massaction class if it is disabled in your configuration.

Another way to customize mass actions is adding a plugin for Magento\Ui\Component\MassAction::prepare.

0

You can add mass actions to Magento 2 admin grid using the following XML in the grid XML file:

<listingToolbar name="listing_top">
    <settings>
        <sticky>true</sticky>
    </settings>
    <bookmark name="bookmarks"/>
    <columnsControls name="columns_controls"/>
    <filters name="listing_filters"/>
    <paging name="listing_paging"/>
    <massaction name="listing_massaction">
        <action name="update_status">
            <settings>
                <type>update_status</type>
                <label translate="true">Update status</label>
                <actions class="{Namespace}\{Module}\Ui\Component\MassAction\Status\Options"/>
            </settings>
        </action>
        <action name="delete">
            <settings>
                <confirm>
                    <message translate="true">Are you sure to delete selected items?</message>
                    <title translate="true">Delete items</title>
                </confirm>
                <url path="{frontname}/{controller}/{action}"/>
                <type>delete</type>
                <label translate="true">Delete</label>
            </settings>
        </action>
        ...
    </massaction>
    ...
</listingToolbar>

You then need to create your own controller and actions class if necessary. How to add mass action to Magento 2 admin grid? explains in detail how to add mass actions to your Magento 2 admin grid.

Your Answer

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

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