0

Is there a way I can disable my custom module according to the system config value? So that the user of my module can just disable the module from frontend by just setting it in admin 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="ccengine" translate="label" sortOrder="10">
        <label>ccengine</label>
    </tab>
    <section id="ccengine" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
        <class>separator-top</class>
        <label>User Config</label>
        <tab>ccengine</tab>
        <resource>Faes_CCEngine::ccengine_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="url" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>URL</label>
                <comment>URL given</comment>
            </field>
            <field id="token" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Token</label>
                <comment>Token given</comment>
            </field>
            <field id="font_family" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Font Family</label>
                 <source_model>Faes\CCEngine\Model\Config\Source\Font</source_model>
                <comment>Font Family on the frontend.</comment>
            </field>
            <field id="primary_color" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Primary Color</label>
                <comment><![CDATA[Primary color]]></comment>
                <frontend_model>Faes\CCEngine\Block\Color</frontend_model>
            </field>
            <field id="secondary_color" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Secondary Color</label>
                <comment><![CDATA[Secondary color]]></comment>
                <frontend_model>Faes\CCEngine\Block\Color</frontend_model>
            </field>
        </group>
    </section>
</system>

0

@Sidharth Satheesh welcome to the community.

To check condition is module is enabled or disable you have to do something like this:

Create helper function in your custom module.

app/code/Namespace/Modulename/Helper/Data.php

const CONFIG_MODULE_IS_ENABLED = 'namespace_modulename/general/enable';

public function isModuleEnabled() {

        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $isEnabled = $this->scopeConfig->getValue(self::CONFIG_MODULE_IS_ENABLED, $storeScope);
        return $isEnabled;
    }

And call this function in frontend where you want.

I hope it helps!

  • Thank You. Yes I will try this, but is there any other way to disable – Sidharth Satheesh Jul 1 at 7:18
  • You can disable an extension in three-way using command 1php bin/magento module:disable Your module name` from etc/config and from system config. – Chirag Patel Jul 1 at 7:28
0

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>

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; ?>
  • This will work perfectly. – P Ramulu Jul 1 at 7:19
  • Perfect. @PRamulu – Sidharth Satheesh Jul 2 at 10:41
  • Please give Up arrow Sidharth Satheesh. Thanks – P Ramulu Jul 3 at 7:29
0

You can use the below code to get that done.

/app/code/Faes/CCEngine/Helper/Data.php

namespace Faes\CCEngine\Helper;

/**
 * Contact base helper
 */
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    const XML_PATH_ENABLED = 'ccengine/general/enable';

    /**
     * Check if enabled
     *
     * @return string|null
     */
    public function isEnabled()
    {
        return $this->scopeConfig->getValue(
            self::XML_PATH_ENABLED,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }

No you can use isEnabled() funciton to apply disabled and enabled.

  • Is there a way I can disable it according to system config value? The user of my module can just disable the module from frontend by just setting it in admin configuration? – Sidharth Satheesh Jul 1 at 7:08
  • Please check I have updated my code – Ravi Soni Jul 1 at 7:30

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.