0

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"
           />
&lt;add id=&quot;Vendor_Chd::menu_submenu1&quot;
       title=&quot;Test sub menu 1&quot; module=&quot;Vendor_Chd&quot;
       sortOrder=&quot;10&quot; parent=&quot;Vendor_Chd::menu&quot;
       action=&quot;chd/create/index&quot;
       resource=&quot;Vendor_Chd::menu_item&quot;
       /&gt;


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


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

$moduleName = 'Vendor_Chd';
$outputPath = &quot;advanced/modules_disable_output/$moduleName&quot;;

$this-&gt;_config-&gt;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

Pramod
  • 1,464
  • 1
  • 11
  • 38

1 Answers1

0

change below code it will work

<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0" canRestore="1">
    <label>Enabled</label>
    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>

In ovserver

$scopeType = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
if(!$this->_scopeConfig->getValue('chd/general/active, $scopeType);){
    return $this;
}
Tirth Patel
  • 1,039
  • 8
  • 26