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>
</section>
</system>
</config>
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; ?>