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="vendor" translate="label" sortOrder="10">
<label>Vendor</label>
</tab>
<section id="module" translate="label" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Module Name</label>
<tab>vendor</tab>
<resource>Vendor_Module::module_configuration</resource>
<group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
<label>General Configuration</label>
<field id="modulestatus" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Module Enable</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Display Text</label>
<comment>This text will display on the frontend.</comment>
</field>
</group>
</section>
</system>
</config>
config.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<module>
<general>
<enable>1</enable>
<display_text>Hello World</display_text>
</general>
</module>
</default>
</config>
Helper/Data.php
<?php
namespace Vendor\Module\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
class Data extends AbstractHelper
{
protected $storeManager;
protected $objectManager;
const XML_PATH_MODULE_GENERAL = 'module/general/';
public function __construct(Context $context, ObjectManagerInterface $objectManager, StoreManagerInterface $storeManager)
{
$this->objectManager = $objectManager;
$this->storeManager = $storeManager;
parent::__construct($context);
}
public function getConfigValue($field, $storeId = null)
{
return $this->scopeConfig->getValue($field, ScopeInterface::SCOPE_STORE, $storeId);
}
public function getModuleStatus($code, $storeId = null)
{
return $this->getConfigValue(self::XML_PATH_MODULE_GENERAL . $code, $storeId);
}
}
Block/Index.php
<?php
namespace Vendor\Module\Block;
use \Magento\Framework\View\Element\Template;
use \Magento\Framework\View\Element\Template\Context;
use \Magento\Store\Model\StoreManagerInterface;
use \Vendor\Module\Helper\Data;
class Index extends Template
{
public function __construct(Context $context, StoreManagerInterface $storeManager, Data $helperData)
{
$this->_storeManager = $storeManager;
$this->_helperData = $helperData;
parent::__construct($context);
}
public function getModuleStatus()
{
return $this->_helperData->getModuleStatus();
}
}
To fetch values, I am using below code in my template file, it displays the module enable/disable status but does not display text
echo $block->getModuleStatus();
echo $block->getModuleStatus('display_text');
- How could I call helper functions via block in template file ?
- How could I add
ifconfig
condition in xml to load block ?