0

Hi am new to magento 2 and learning how to to create custom modules and i have added a option to enable disable from drop down and also added a config file but that seems to be not working. can anyone help me out.

am sharing my code:-

CONFIG.XML

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../Store/etc/config.xsd">
 <default>
     <custom_module>
       <general>
           <enable>0</enable>
              <title>Custom Customer Attribute</title>
         </general>
     </custom_module>
 </default>
</config>

SYESTEM.XML

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
    <system>
    <tab id="mytab" translate="label" sortOrder="200">
        <label>Eecom</label>
    </tab>

    <section id="custom_module" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
        <class>separator-top</class>
        <label>Custom Customer Attribute</label>
        <tab>mytab</tab>
        <resource>Eecom_CustomCustomerAttribute::config_custom_module</resource>
        <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>General Configuration</label>
            <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Enable Module</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field> 
        </group>
  </section>
</system>
</config>

Helper/Data.php

<?php
/**
 * Copyright © 2015 Eecom . All rights reserved.
 */
namespace Eecom\CustomCustomerAttribute\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    public function __construct(\Magento\Framework\App\Helper\Context $context
    ) {
        parent::__construct($context);
    }

    public function isEnabled()
{
    return (boolean) $this->getConfig('eecom_customcustomerattribute/general/enable');
}
}
Pramod
  • 1,464
  • 1
  • 11
  • 38

4 Answers4

0

Vendor\Extension\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="mymodule" sortOrder="150" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Mymodule Label</label>
      <tab>mymoduletab</tab>
      <resource>Module_First::mymodule_configuration</resource>
      <group id="general" translate="label" sortOrder="10" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>General Configuration</label>
        <field id="enable" translate="label" sortOrder="10" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Enable</label>
          <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
        </field>
      </group>
    </section>
  </system>
</config>

And to check that field value in phtml you can create Helper file (Data.php) at this location Vendor/Extension/Helper/Data.php.

<?php
namespace Vendor\Extension\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;

class Data extends AbstractHelper
{
    protected $context;

    public function __construct(Context $context)
    {
        $this->context = $context;
        parent::__construct($context);
    }

    public function isEnable()
    {
        return $this->scopeConfig->getValue('mymodule/general/enable', ScopeInterface::SCOPE_STORE);
    }
}

Now Write this code in phtml file :

<?php 
$helper = \Magento\Framework\App\ObjectManager::getInstance()->get('Vendor\Extension\Helper\Data'); 
if($helper->isEnable()) : ?>

//WRITE YOUR CODE HERE

<?php endif; ?>

And more refer this link :-

Magento 2 - add Enable / Disable field for custom module

may be help this code ..

Mohit Patel
  • 3,778
  • 4
  • 22
  • 52
0

system.xml file

<?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="mytab" translate="custom Configuration" sortOrder="100" class="trademe-extensions-tab">
            <label>Custom Customer Attribute</label>
        </tab>
        <section id="custom_module" translate="TradeMe Settings" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Custom Customer Attribute</label>
            <tab>mytab</tab>
            <resource>Eecom_CustomCustomerAttribute::custom_module</resource>
            <group id="general" translate="TradeMe Configuration" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enable Module</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field> 
        </group>
  </section>
</system>
</config>

Controller file

protected $scopeConfig;

const CUSTOM_MODULE_ENABLE = 'custom_module/general/enable';

public function __construct(
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
) {
    $this->scopeConfig = $scopeConfig;
}

public function execute()
{
    echo $isEnable  = $this->scopeConfig->getValue(self::CUSTOM_MODULE_ENABLE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
}
Milan Maniya
  • 330
  • 3
  • 11
0

Your Helper File replace by this code

<?php

namespace Eecom\CustomCustomerAttribute\Helper;

use Magento\Store\Model\ScopeInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{

    const MODULE_ENABLE_DISABLE = 'custom_module/general/enable'; // SectionName/GroupName/FieldNAme from system.xml

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    public function setStoreScope()
    {
        return ScopeInterface::SCOPE_STORE;
    }
    public function isEnabled()
    {
        return $this->scopeConfig->getValue(static::MODULE_ENABLE_DISABLE, $this->setStoreScope());
    }
}

Note: module is not directly enable or disable from system.xml to field. we are provide admin can easily interact with our module our module provide more flexibility using system.xml

For Understanding system.xml click here

Msquare
  • 9,063
  • 7
  • 25
  • 63
0

I would prefer to disable the module via terminal

bin/magento module:disable ${MODULE_NAME}

If it says there is no such module, try listing all enabled ones to find yours

bin/magento module:status --enabled

Reference to official dosc