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);
}
}
-
Can you please add the code of System.xml file ?Abhishek Panchal– Abhishek Panchal2018年02月21日 14:59:50 +00:00Commented Feb 21, 2018 at 14:59
2 Answers 2
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
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.
-
if($this->helperData->isEnable()){ echo "enabled"; }else{ echo "not enbaled"; }deepak– deepak2018年02月21日 14:56:45 +00:00Commented Feb 21, 2018 at 14:56
-
every time echo not enabledeepak– deepak2018年02月21日 14:57:12 +00:00Commented Feb 21, 2018 at 14:57
-
I thing data is not coming from helper > Data.phpdeepak– deepak2018年02月21日 14:57:50 +00:00Commented Feb 21, 2018 at 14:57
-
I have seen every time it print 1deepak– deepak2018年02月21日 15:16:00 +00:00Commented Feb 21, 2018 at 15:16
-
check my updated answer and edit your helper file to declare
scopeConfigvariableAbhishek Panchal– Abhishek Panchal2018年02月21日 15:17:05 +00:00Commented Feb 21, 2018 at 15:17