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');
}
}

4 Answers 4

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 ..

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);
}
4
  • inside adminhtml directory??
    – Pramod
    Mar 24, 2020 at 11:42
  • You can add any Action, Block and Helper File. Mar 24, 2020 at 11:48
  • i have already tried this but it didint work check my helper code
    – Pramod
    Mar 24, 2020 at 12:13
  • You can add my code in the helper file and try it. Mar 25, 2020 at 6:25
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

0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.