4

I am trying to add a mass action item to the product grid mass action block using an observer.

Here is what I am doing but it does not seem to be working, I feel like I am missing something

app/code/DigitalPianism/ProductExport/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="DigitalPianism_ProductExport" setup_version="0.0.1" />
</config>

app/code/DigitalPianism/ProductExport/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'DigitalPianism_ProductExport',
    __DIR__
);

app/code/DigitalPianism/ProductExport/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="adminhtml_catalog_product_grid_prepare_massaction">
        <observer name="digitalpianism_productexport_add" instance="DigitalPianism\ProductExport\Observer\AddMassExportAction" />
    </event>
</config>

app/code/DigitalPianism/ProductExport/Observer/AddMassExportAction.php

<?php
namespace DigitalPianism\ProductExport\Observer;

use Magento\Framework\Event\ObserverInterface;

class AddMassExportAction extends ObserverInterface
{

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $block = $observer->getEvent()->getBlock();

        $block->getMassactionBlock()->addItem(
            'productexport',
            [
                'label' => __('Export to CSV'),
                'url' => $block->getUrl('productExport/productexport/exportCsv')
            ]
        );
    }
}

Thanks.

  • I am trying to add a massaction depending upon the system configuration through the observer, but the event observer doesnt work. Is there any solution for it ? – Manashvi Birla Nov 30 '16 at 12:01
  • @Manashvibirla you don't need to use observers anymore, it's done directly via the UI Components (see the answers below) – Raphael at Digital Pianism Nov 30 '16 at 12:04
  • 1
    I got that. But what if i want to add a massAction only if a certain configuration is selected in the System configuration ? – Manashvi Birla Nov 30 '16 at 12:07
  • @Manashvibirla not sure to be honest, feel free to create a new question – Raphael at Digital Pianism Nov 30 '16 at 12:12
14

Please note that since Magento 2.1.x, the XML has slightly changed. You now have to use <listingToolbar/> instead of <container/>:

<listingToolbar name="listing_top">
    <massaction name="listing_massaction">
        <action name="export">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="type" xsi:type="string">export</item>
                    <item name="label" xsi:type="string" translate="true">Export to CSV</item>
                    <item name="url" xsi:type="string">productExport/productexport/exportCsv</item>
                </item>
            </argument>
        </action>
    </massaction>
</listingToolbar>
  • What if you wanted to make this action conditional based on the given row? For example a grid with an action dropdown with actions like edit, cancel, etc, but only if the item is editable or cancelable based on ACL? – Wouter Dec 24 '18 at 13:29
5

Answering my own question.

For Magento 2.0

The right way to add a mass action item to the Catalog grid in Magento is by creating the following file:

app/code/DigitalPianism/ProductExport/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">
    <container name="listing_top">
        <massaction name="listing_massaction">
            <action name="export">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">export</item>
                        <item name="label" xsi:type="string" translate="true">Export to CSV</item>
                        <item name="url" xsi:type="string">productExport/productexport/exportCsv</item>
                    </item>
                </argument>
            </action>
        </massaction>
    </container>
</listing>

For Magento 2.1

See Giel Berkers answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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