2

I have created a New module in Magneto 2 Which is Working well Now I want to control the status of the module on the bases of system Configuration setting

How can I do this?

I have already created a Field for status enable/disable in my system.xml

2 Answers 2

7

syetm.xml file add custom filed for module configuration 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="mytab" translate="label" sortOrder="500">
        <label>My Custom Extensions</label>
    </tab>

    <section id="custom_module" translate="label" type="text" sortOrder="300" showInDefault="1" showInWebsite="1" showInStore="1">
        <class>separator-top</class>
        <label>My Custom Module</label>
        <tab>mytab</tab>
        <resource>My_Module::config_my_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>

custom helper to check module enable / disable based on system configuration My\Module\Helper\Data

<?php
namespace My\Module\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
   const MODULE_ENABLE = "custom_module/general/enable";

   public function getDefaultConfig($path)
   {
       return $this->scopeConfig->getValue($path, \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
   }

   public function isModuleEnabled()
   {
       return (bool) $this->getDefaultConfig(self::MODULE_ENABLE);
   }
 }

Block to get module configuration value My\Module\Block\Product\View

<?php
namespace My\Module\Block\Product;

class View extends \Magento\Framework\View\Element\Template
{
   /**
     * @var \My\Module\Helper\Data
   */
   protected $_dataHelper;

 /**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \My\Module\Helper\Data $dataHelper
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \My\Module\Helper\Data $dataHelper,
    array $data = []
) {
    $this->_dataHelper = $dataHelper;
    parent::__construct($context, $data);
}

public function canShowBlock()
{
    return $this->_dataHelper->isModuleEnabled();
}
}

template file display content based on the return value from block

<?php if ($block->canShowBlock()): ?>
   <h1> My Mdule content </h1>
<?php endif; ?>
1
  • That what i exactly need... THank You
    – Waqar Ali
    Apr 24, 2019 at 4:39
0

You need to make the backend configuration with the help of system.xml .In your module include the file system.xml in your etc folder. You can create a section in backend that can contains all the configuration you need to control from backend.

You can use below links for getting some idea and work accordingly

Magento 2 - add Enable / Disable field for custom module

Add module Enable/disable functionality in Magento 2

Magento 2 - How to create a custom module with admin config?

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.