0

I would like to add option in Configuration for module Enabled Yes/No.

I have tried below code but not working. Can you please suggest me any change on it or need to add any anther file.

<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
        <label>Enabled</label>
        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
2
  • any error you face? Aug 4, 2017 at 9:04
  • Not any Error, Just I put Code in syste.xml file and in Configuration I have set No . Still module data show into front-end. Aug 4, 2017 at 9:09

1 Answer 1

2

Add in your layout when phtml called use ifconfig="sectionid/groupid/fieldid" like this

<block class="Magento\Framework\View\Element\Template" template="vendorname_Modulename::your.phtml" ifconfig="sectionid/groupid/fieldid"/>

You can check module enable or disable in controller,block

<?php
namespace Companyname\Modulename\Controller\ControllerFolder;

class Index extends \Magento\Framework\App\Action\Action
{

    private $scopeConfig;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\App\Action\Context $context
    ) {

        parent::__construct($context);
        $this->scopeConfig = $scopeConfig;
    }

    public function execute()
    {
        $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
        $enableConfig = $this->scopeConfig->getValue('sectionid/groupid/sectionid', $storeScope);
        if ($enableConfig) {
            $this->_view->loadLayout();
            $this->_view->renderLayout();
        } else {
            $this->_redirect('404');
        }
    }
}
2
  • Thanks for ans, This is also useful for my other question . But This is not useful for module disable. When I select No module is not showing in front-end like URL go with 404. Aug 4, 2017 at 10:04
  • 1
    @HiteshVaghasiya i have update answer check it Aug 4, 2017 at 10:09

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.