Hi i have a custom module where am trying to add enable/disable functionality but its not working can anyone suggest me a possible solution am sharing my code below
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="chd" translate="label" sortOrder="150" showInDefault="1" showInWebsite="1" showInStore="1">
<resource>Vendor_Chd::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>
menu.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Vendor_Chd::menu"
title="Ntz Test Menu" module="Vendor_Chd"
sortOrder="10"
resource="Magento_Backend::content"
/>
<add id="Vendor_Chd::menu_submenu1"
title="Test sub menu 1" module="Vendor_Chd"
sortOrder="10" parent="Vendor_Chd::menu"
action="chd/create/index"
resource="Vendor_Chd::menu_item"
/>
<!-- settings -->
<add id="Vendor_Chd::menu_submenusettingsenabledisable"
title="Settings"
resource="Magento_Backend::content"
module="Vendor_Chd"
sortOrder="1"
action="adminhtml/system_config/edit/section/chd"
parent="Vendor_Chd::menu" />
</menu>
</config>
Disableoutput.php
<?php
namespace Vendor\Chd\Observer;
use Magento\Config\Model\ResourceModel\Config;
class DisableOutput implements \Magento\Framework\Event\ObserverInterface
{
const VENDOR_CONFIG = 'chd/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_Chd';
$outputPath = "advanced/modules_disable_output/$moduleName";
$this->_config->saveConfig($outputPath,$disable, $scopeType,$scopeCode);
}
}
event.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_chd_disable" instance="Vendor\Chd\Observer\DisableOutput" />
</event>
</config>
any advice would be appreciated