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
3 Answers 3
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>
-
Hi @Guxholli Did the above code is worked for you.shivashankar m– shivashankar m2019年05月17日 10:08:25 +00:00Commented May 17, 2019 at 10:08
-
Yes it worked @shivashankarmYlgen Guxholli– Ylgen Guxholli2019年05月20日 08:33:19 +00:00Commented May 20, 2019 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.shivashankar m– shivashankar m2019年05月27日 10:13:23 +00:00Commented May 27, 2019 at 10:13
-
@shivashankarm i didnt understand your question . Explain in more detailsYlgen Guxholli– Ylgen Guxholli2019年06月14日 07:32:47 +00:00Commented Jun 14, 2019 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:configapedic– apedic2019年07月09日 12:34:51 +00:00Commented Jul 9, 2019 at 12:34
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
-
Oh, I think I misunderstood his questionfmsthird– fmsthird2019年01月15日 12:50:22 +00:00Commented Jan 15, 2019 at 12:50
-
Sorry, what is mean of requirement2019年01月15日 12:52:52 +00:00Commented Jan 15, 2019 at 12:52
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
-
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 statusAkash Agrawal– Akash Agrawal2019年01月15日 13:16:14 +00:00Commented Jan 15, 2019 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– Abid Malik2019年10月10日 11:21:15 +00:00Commented Oct 10, 2019 at 11:21