TransWikia.com

Execute Plugin Class from specific module in Magento2

Magento Asked by Manjunath on December 24, 2021

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="MagentoUiModelExportMetadataProvider">
        <plugin name="custom_export1" type="VendorReport1PluginModifyExportPlugin" sortOrder="1" disabled="false"/>
    </type>

I have used the same plugin in another 4 modules.

Vendor/Report2//etc/di.xml

<type name="MagentoUiModelExportMetadataProvider">
        <plugin name="custom_export2" type="VendorReport2PluginModifyExportPlugin" 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!!

One Answer

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 VendorReport1Plugin;
    
class ModifyExportPlugin
{
    protected $request;
    
    public function __construct(
       MagentoFrameworkAppRequestInterface $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 VendorReport1ControllerAdminhtmlExport;

class GridToCsv extends MagentoUiControllerAdminhtmlExportGridToCsv
{   
        
}

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.

Answered by HelgeB on December 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP