0

I am trying to enable/disable Magento 2 from admin general setting. I created an option to disabled / enabled module from backend > configuration > module > enable (Yes/No)

when I tried to disabled module functionality still working. Is it a database related issue or another issue.

Secondly, I tried to get check in coding whether module enable or not by followed below steps

Index controller :-

<?php  
namespace Vender\Modulename\Controller\Index;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action 
   {
      protected $resultJsonFactory;
      protected $scopeConfig;

      public function __construct(Context $context, 
      \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
     \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory)
        {
           $this->resultJsonFactory  = $resultJsonFactory;
         //$this->_helper = $helper;
           $this->scopeConfig = $scopeConfig;
           parent::__construct($context);
        }
      protected function helper($className) 
      {
         return $this->_objectManager->get($className);
       }

      protected function getUrl($path) 
     {
      return $this->_objectManager->get('Magento\Framework\UrlInterface')->getUrl($path);
      }

     public function execute()
     {
    //die('dfsd');
    //$topTel = $this->_helper->getConfig('dynamicpdf/general/topTel');
    //$_thishepler = $this->_helper('Vender\Modulename\Helper\Data');
    //$isEnabled = $helper->getStoreConfig('VenderMOdulename/general/enabled');
    //print_r($isEnabled); die;
    $helper = \Magento\Framework\App\ObjectManager::getInstance()->get('Vendor\modulename\Helper\Data');

    echo "<pre>";
    //print_r($helper);

    if($helper->isEnable()){
    }
          // My code
  }

}

Helper > Data.php

namespace Vendor\Modulename\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('modulename/general/enable', ScopeInterface::SCOPE_STORE);
   }
}
1
  • Can you please add the code of System.xml file ? Commented Feb 21, 2018 at 14:59

2 Answers 2

0

If you enabled/disabled one or more modules, then you will need to run magento setup:upgrade to update the database schema.

Try running commands:

php bin/magento setup:upgrade
php bin/magento c:c
0

Use below code in your Index Controller file.

protected $helperData;

public function __construct(
    \Vendor\Modulename\Helper\Data $helperData
) {
    $this->helperData = $helperData;
}

then use below code to call your helper function.

$this->helperData->isEnable();

EDIT: in your helper file you didn't declare variable

protected $scopeConfig;

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

This is the proper way to call helper function/method instead using Object Manager.

Don't forget to flush cache.

6
  • if($this->helperData->isEnable()){ echo "enabled"; }else{ echo "not enbaled"; }
    – deepak
    Commented Feb 21, 2018 at 14:56
  • every time echo not enable
    – deepak
    Commented Feb 21, 2018 at 14:57
  • I thing data is not coming from helper > Data.php
    – deepak
    Commented Feb 21, 2018 at 14:57
  • I have seen every time it print 1
    – deepak
    Commented Feb 21, 2018 at 15:16
  • check my updated answer and edit your helper file to declare scopeConfig variable Commented Feb 21, 2018 at 15:17

Your Answer

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

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