How to add Enable/Disable extention functionality in admin side in custom extention magento 2.3
-
04-03-2021 - |
Pregunta
We want to create one custom extension which has functionality in admin side for Enable/Disable. Any one help me how to add this functionality in my custom extension.
Solución
For this, you need to Create a System.xml in the folder like 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>
<tab id="ModuleName" translate="label" sortOrder="10">
<label>ModuleName</label>
</tab>
<section id="customproductdata" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Custom Product Data</label>
<tab>ModuleName</tab>
<resource>ModuleName_Customproductdata::customproductdata_configuration</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>General Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
Than Create Config.xml file as the Same location and Put Below code into that.
<?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>
<customproductdata>
<general>
<enable>1</enable>
<title>ModuleName Customproductdata</title>
</general>
</customproductdata>
</default>
</config>
Now you will See this custom Yes no option in Magento 2 admin section.
Otros consejos
Create system.xml in your module.
etc/adminhtml/system.xml
Add the following content to have enable disable module configuration.
<?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>
Add below code in etc/adminhtml/system.xml
<group id="general" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="20">
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1">
<label>Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
Using below code in helper file.
public function isEnabled()
{
return (boolean) $this->getConfig('namespace_modulename/general/enable');
}
And call this function whenever you want. That's it, i hope it helps!
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange