3

I want to add Enable/disable functionality in my custom module by which the module can be enabled or disable directly form admin-panel for just selecting.

I want to add something like this in my custom module enter image description here

2

This type of functionality does not exist at Magento.

If you want to develop this functionality on your module then you have to add an If conditions of checking this system configuration field on every code of this module. That is too complex and too tough to implement this logic on any module code.

Also, Implementation of this system field checking on a module totally depends on the module code and its business logic and how the business logic is implemented On the code.

Be a developer, my suggestion, you cannot possibly implement this type of enable/disable of a module from admin

2
  • Oh, I think I misunderstood his question
    – fmsthird
    Jan 15 '19 at 12:50
  • Sorry, what is mean of requirement
    – Amit Bera
    Jan 15 '19 at 12:52
2

Modify this files based on your module (update module namespace)

Vendor/YourModuleName/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="admin_system_config_changed_section_yourmodulename">
        <observer name="vendor_yourmodulename_disable" instance="Vendor\YourModuleName\Observer\DisableOutput" />
    </event>
</config>

Vendor/YourModuleName/Observer/DisableOutput.php

<?php
namespace Vendor\YourModuleName\Observer;

use Magento\Config\Model\ResourceModel\Config;

class DisableOutput implements \Magento\Framework\Event\ObserverInterface
{

const VENDOR_CONFIG = 'yourmodulename/general/enable';

/**
 * @var \Magento\Config\Model\ResourceModel\Config
 */
protected $_config;

/**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 */
protected $_scopeConfig;

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $storeManager;


/**
 * DisableOutput constructor.
 * @param \Magento\Config\Model\ResourceModel\Config $_config
 * @param \Magento\Framework\App\Config\ScopeConfigInterface $_scopeConfig
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 * @param \Magento\Framework\App\RequestInterface $request
 */
public function  __construct(
    Config $_config,
    \Magento\Framework\App\Config\ScopeConfigInterface $_scopeConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\App\RequestInterface $request
){
    $this->_config = $_config;
    $this->_scopeConfig = $_scopeConfig;
    $this->storeManager = $storeManager;
    $this->request = $request;

}

/**
 * @param \Magento\Framework\Event\Observer $observer
 */
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $disable = false;
    $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
    $scopeCode = 0;

    if($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE))
    {
        $scopeType = ScopeInterface::SCOPE_STORE;
        $scopeCode = $this->storeManager->getStore($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_STORE))->getCode();
    }elseif($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE))
    {
        $scopeType = ScopeInterface::SCOPE_WEBSITE;
        $scopeCode = $this->storeManager->getWebsite($this->request->getParam(\Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE))->getCode();
    }
    else
    {
        $scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
        $scopeCode = 0;
    }
    $moduleConfig= $this->_scopeConfig->getValue(self::VENDOR_CONFIG, $scopeType);


    if((int)$moduleConfig == 0){
        $disable = true;
    }

    $moduleName = 'Vendor_YourModuleName';
    $outputPath = "advanced/modules_disable_output/$moduleName";

    $this->_config->saveConfig($outputPath,$disable, $scopeType,$scopeCode);
}
}

etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="yourmodulename" translate="label" sortOrder="150" showInDefault="1" showInWebsite="1" showInStore="1">
            <resource>Vendor_YourModuleName::configuration</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Enable Module</label>
                    <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

etc/adminhtml/menu.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../Backend/etc/menu.xsd">
    <menu>
        <add id="Vendor_YourModuleName::yourmodulename_configuration"
             title="Configuration"
             resource="Magento_Backend::content"
             module="Vendor_YourModuleName"
             sortOrder="1"
             action="adminhtml/system_config/edit/section/yourmodulename"
             parent="Vendor_YourModuleName::yourmodulename" />
    </menu>
</config>
5
  • Hi @Guxholli Did the above code is worked for you. May 17 '19 at 10:08
  • Yes it worked @shivashankarm May 20 '19 at 8:33
  • Thanks for your reply @Ylgen, i have lot of files in my module, Is there any need to add above functionality for every file. May 27 '19 at 10:13
  • @shivashankarm i didnt understand your question . Explain in more details Jun 14 '19 at 7:32
  • nice one but pay attention as this is going to disable the "output" functionalities only. all the others functionalities will work as usual, so the only way is to disable via module:config Jul 9 '19 at 12:34
0

Create a system Config file in your custom module

You can do like this (Assuming your custom module name is "Sample Shipping"

Vendor\Module\etc\adminhtml\system.xml

system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <section id="carriers" translate="label" type="text" sortOrder="320" showInDefault="1" showInWebsite="1" showInStore="1">
        <group id="sampleShipping" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Sample Shipping</label>
            <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
                <label>Enable</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
        </group>
    </section>
</system>

Vendor\Module\etc\config.xml

config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
    <carriers>
        <sampleShipping>
            <version>1.0.0</version>
            <active>1</active>
            <sallowspecific>0</sallowspecific>
        </sampleShipping>
    </carriers>
</default>

Creating those files in your custom module will enable you to disable/enable your custom module in
Stores > Sales > Shipping method configuration

2
  • Thanks For help! Yes Your code creating the option for Enable and disable, but it seems not working, when I change the status module always remains at same status Jan 15 '19 at 13:16
  • @AkashAgrawal after implementing above code you need to write php code in your class to make sure if module is enabled.
    – Abid Malik
    Oct 10 '19 at 11:21

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.