0

Hi i am new to magento and currently learning custom module for most viewed product and the module works great. now i have added a system.xml file below is the image.

enter image description here

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="netsmartz" translate="label" sortOrder="10">
            <label>Netsmartz</label>
        </tab>
        <section id="mostviewed" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Most Viewed</label>
            <tab>netsmartz</tab>
            <resource>Netsmartz_Mostviewed::mostviewed_config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Name</label>
                    <comment>
                        Enable the module to get the most viewed sorting option in frontend
                    </comment>
                </field>
            </group>
        </section>
    </system>
</config>

config.xml

<?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>   
                <Netsmartz_Mostviewed>
                        <general>
                            <use_cdn>1</use_cdn>
                                <display_text>Most Viewed</display_text>
                        </general>
                </Netsmartz_Mostviewed>
    </default>
</config>

now i want that when i hit module disable to no my module should be disable anyone have any idea how to achieve this. Thanks in Advance!

0

This does not work automatically.
What you can do is to wrap in all the methods that affect the core funtionality from your module in an if statement.

private $scopeConfig; //this shold be an instance of \Magento\Framework\App\Config\ScopeConfigInterface
public function doSomething()
{
    if ($this->scopeConfig->isSetFlag('path_to/your/config_field')) {
        //do what the method should do
    }
}
| improve this answer | |
0

In Your 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="netsmartz" translate="label" sortOrder="10">
            <label>Netsmartz</label>
        </tab>
        <section id="mostviewed" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Most Viewed</label>
            <tab>netsmartz</tab>
            <resource>Netsmartz_Mostviewed::mostviewed_config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Name</label>
                    <comment>
                        Enable the module to get the most viewed sorting option in frontend
                    </comment>
                </field>
            </group>
        </section>
    </system>
</config>

in Your Vendor\Module\Helper\Data.php

    <?php
    namespace My\Module\Helper;

    class Data extends \Magento\Framework\App\Helper\AbstractHelper
    {
       const MODULE_ENABLE = "mostviewed/mostviewed/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);
       }
     }

Now Any of you file where you want to put Module enable disable condition

<?php
 protected $_dataHelper;

 public function __construct(
        \Vendor\Module\Helper\Data $dataHelper
    ) {
        $this->_dataHelper = $dataHelper;
    }

    public function canShowBlock()
    {
        if($this->_dataHelper->isModuleEnabled()){
          // Your On Module Enabled Code
        }
    }

}
| improve this answer | |
  • what would be the path of the Block file – Pramod Jan 30 at 10:41
  • i just gave you an example.. you can this function anywhere.. to set condition – Waqar Ali Jan 30 at 10:46
  • check my updated answer – Waqar Ali Jan 30 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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