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.
3 Answers
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.
-
-
If answer was correct or satisfy with answer please approved answer. Commented Jun 14, 2019 at 9:12
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>
-
In admin site where this setting show ? Enable or Disable Commented Jun 13, 2019 at 14:04
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!
-
If any possible way to create extension with enable/disable option ?– AnilCommented Jun 13, 2019 at 14:44
-
I have a post answer for that. but I think you are confused. can you explain more what do you want exactly? Commented Jun 14, 2019 at 4:38
-
we want to create one custom extension. Which in manage from admin side. It means any person enable/disable it from admin side. Main purpose for create this extension is overwrite phtml files for product view page.– AnilCommented Jun 14, 2019 at 5:53