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);
 }
}
Teja Bhagavan Kollepara
3,8275 gold badges33 silver badges69 bronze badges
asked Feb 21, 2018 at 14:27
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
Abhishek Panchal
4,9643 gold badges22 silver badges39 bronze badges
answered Feb 21, 2018 at 14:43
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.

answered Feb 21, 2018 at 14:44
6
  • if($this->helperData->isEnable()){ echo "enabled"; }else{ echo "not enbaled"; } Commented Feb 21, 2018 at 14:56
  • every time echo not enable Commented Feb 21, 2018 at 14:57
  • I thing data is not coming from helper > Data.php Commented Feb 21, 2018 at 14:57
  • I have seen every time it print 1 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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.