#StackBounty: #module #uicomponent #plugin #system.xml #magento-2.2.5 Custom Module for Custom Column using Plugin Yes/No option

Bounty: 50

I have been digging all day trying to figure out a way to enable/disable the plugins of my custom module with system configuration yes/no. Basically, I have a custom module that adds the Company Column to all Admin Order Grids as well as Customer Grids.

I have created the system.xml file to display the options on the backend, including an option to disable the entire module or just certain Order Grids (Order, Invoice, Shipment, etc).

system.xml:

<system>
    <tab id="trucatch" translate="label" sortOrder="500">
        <label><![CDATA[Trucatch]]></label>
    </tab>
    <section id="companycolumn" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
        <label><![CDATA[Company Column]]></label>
        <tab>trucatch</tab>
        <resource>Trucatch_CompanyColumn::companycolumn</resource>
        <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>General</label>
            <field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                <label>Enabled</label><source_model>MagentoConfigModelConfigSourceYesno</source_model>
                <comment><![CDATA[Enable to allow Company Column on Sales Grids]]></comment>
            </field>
            <field id="order_grid_enabled" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
                <label>Show on Order Grid</label><source_model>MagentoConfigModelConfigSourceYesno</source_model>
                <comment><![CDATA[]]></comment>
                <depends>
                    <field id="*/*/enabled">1</field>
                </depends>
            </field>
        </group>
    </section>
</system> 

config.xml:

<default>
    <companycolumn>
        <general>
            <enabled>1</enabled>
            <order_grid_enabled>1</order_grid_enabled>
        </general>
    </companycolumn>
</default>

di.xml is calling all the plugins.

Sample:

<type name="MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory">
    <plugin 
        name="sales_order_additional_columns" 
        type="TrucatchCompanyColumnPluginAddColumnsSalesOrderGridCollection" 
        sortOrder="100" 
        disabled="false" 
    />
</type>

I have also created a HelperData.php file to check, but I have not been able to associate that with the actual plugin file. Or am I looking in the wrong place and this needs to be implemented in the layout/ui_component files?

Data.php

namespace TrucatchCompanyColumnHelper;

use MagentoFrameworkAppHelperAbstractHelper;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkAppHelperContext
use MagentoStoreModelScopeInterface;

class Data extends AbstractHelper
{
/**
 * Config path to enable/disable module
 */
const CONFIG_PATH_GENERAL_ENABLE_MODULE = 'companycolumn/general/enabled';

/**
 * Config path to enable/disable module
 */
const CONFIG_PATH_GENERAL_ORDER_GRID = 'companycolumn/general/order_grid_enabled';

protected $_scopeConfig;

public function __construct(
    Context $context,
    MagentoBackendModelUrlInterface $backendUrl,
    ScopeConfigInterface $scopeConfig
) {
    parent::__construct($context);
    $this->_backendUrl = $backendUrl;
    $this->_scopeConfig = $scopeConfig;
}

public function isEnabled()
{
    return $this->scopeConfig->getValue(
        self::CONFIG_PATH_GENERAL_ENABLE_MODULE,
        MagentoStoreModelScopeInterface::SCOPE_STORE
    );
}

public function isOrderGridEnabled()
{
    return (bool) $this->getConfigValue(
        self::CONFIG_PATH_GENERAL_ORDER_GRID,
        MagenotStoreModelScopeInterface::SCOPE_STORE
    );
}

AddColumnsSalesOrderGridCollection.php:

<?php 

namespace TrucatchCompanyColumnPlugin;

class AddColumnsSalesOrderGridCollection
{
private $resource;

public function __construct(
    MagentoFrameworkAppResourceConnection $resource
) {
    $this->resource = $resource;
}

public function aroundGetReport(
    MagentoFrameworkViewElementUiComponentDataProviderCollectionFactory $subject,
    Closure $proceed,
    $requestName
) {
    $result = $proceed($requestName);
    if ($requestName == 'sales_order_grid_data_source') {
        if ($result instanceof MagentoSalesModelResourceModelOrderGridCollection) {
            $select = $result->getSelect();
            $select->joinLeft(
                ['soa1' => $this->resource->getTableName('sales_order_address')],
                'main_table.entity_id = soa1.parent_id AND soa1.address_type = "billing"',
                ['billing_company' => 'company']
            );
            $select->joinLeft(
                ['soa2' => $this->resource->getTableName('sales_order_address')],
                'main_table.entity_id = soa2.parent_id AND soa2.address_type = "shipping"',
                ['shipping_company' => 'company']
            );
        }
    }
    return $result;
}
}

And Lastly the sales_order_grid.xml:

<!-- "sales_order_grid_data_source" -->
<columns name="sales_order_columns">

    <!-- sales_order_address billing_company -->
    <column name="billing_company">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Bill-to Company</item>
                <item name="sortOrder" xsi:type="number">4</item>
                <item name="align" xsi:type="string">left</item>
                <item name="filter" xsi:type="string">text</item>
                <item name="visible" xsi:type="boolean">true</item>
            </item>
        </argument>
    </column>

    <!-- sales_order_address shipping_company -->
    <column name="shipping_company">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Ship-to Company</item>
                <item name="sortOrder" xsi:type="number">6</item>
                <item name="align" xsi:type="string">left</item>
                <item name="filter" xsi:type="string">text</item>
                <item name="visible" xsi:type="boolean">true</item>
            </item>
        </argument>
    </column>

</columns>

I am looking to be able to enable/disable certains aspects of this module. For instance you could disable the Company Column from the Sales Order Grid (all information associated with that is above). But I can’t seem to grasp how to implement it into my plugins, if it is even possible.

I do understand that you can hide columns and such in Magento2 but I thought this would be an easy way to implement it as well. This way it would not load them for Filters also, but I am still working on getting the Filtering to work.

Please let me know if you need anything else to better assist me.

Thanks!


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.