Question

I have created a custom reports using ui components.

For the export csv I have created a plugin class for this function.

Vendor/Report1//etc/di.xml

<type name="Magento\Ui\Model\Export\MetadataProvider">
        <plugin name="custom_export1" type="Vendor\Report1\Plugin\ModifyExportPlugin" sortOrder="1" disabled="false"/>
    </type>

I have used the same plugin in another 4 modules.

Vendor/Report2//etc/di.xml

<type name="Magento\Ui\Model\Export\MetadataProvider">
        <plugin name="custom_export2" type="Vendor\Report2\Plugin\ModifyExportPlugin" sortOrder="1" disabled="false"/>
    </type>

From the above code, if i export from any of the module, both the plugins are called, I need to avoid that,

When export is happened from Module Report1,the plugin from that module to be executed, if called from Report2, the plugin defined in that module need to call.

How this can be achieved? Can anyone help me to resolve this issue. Thanks in advance!!

Was it helpful?

Solution

I'm not sure if I understood your problem, but you may check the modulename of the request in your plugin methods and execute the code only if the request module name matches.

For example adding some code like this in your plugins may help you solving the problem (example for module Report1):

namespace Vendor\Report1\Plugin;
    
class ModifyExportPlugin
{
    protected $request;
    
    public function __construct(
       \Magento\Framework\App\RequestInterface $request
        
    ) {
       $this->request = $request;
    }
    
    //check the current request module name in your plugin functions
    if (request->getModuleName() == 'report1'){
        //place your plugin code here...
    }
}

In addition to have module specific controllers (for gridToCsv and gridToXml) you should create for each module a controller extending the original controller of the Ui module. Example for GridToCsv for module Report1 (place this content into GridToCsv.php in Controller/Adminhtml/Export directory):

namespace Vendor\Report1\Controller\Adminhtml\Export;

class GridToCsv extends \Magento\Ui\Controller\Adminhtml\Export\GridToCsv
{   
        
}

Create a routes.xml file in etc/adminthtml of your module (if it not exists already). Example for module Report1:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route frontName="report1" id="report1">
            <module name="Vendor_Report1" before="Magento_Backend" />
        </route>
    </router>
</config>

The definition of the ui component should be updated for each module like in this answer How to override definition.xml file in Magento 2? Especially the urlparameter should be like this according to the code above (Example for module Report1 and action gridToCsv):

<item name="url" xsi:type="string">report1/export/gridToCsv</item>

If you make these changes for all the affected modules, the approach should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top