0

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 ?

2 Answers 2

2

I got it resolved by changing code to below

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($code)
       {

        return $this->_helperData->getModuleStatus($code);
        }

    }

Fetch value using below

echo $block->getModuleStatus('modulestatus');
echo $block->getModuleStatus('display_text');

To use it via xml

ifconfig = section_id/group/field

<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="config_path/group/field" name="contact-us-link">
        <arguments>
            <argument name="label" xsi:type="string" translate="true">Contact Us</argument>
            <argument name="path" xsi:type="string">contact</argument>
        </arguments>
</block>
1

Since you have defined the function in your helper file you need to pass the config path that you want to retrieve.

In your block

public function getModuleStatus()
{
    return $this->_helperData->getModuleStatus('modulestatus');
}

In your template file you can directly call the function

echo $block->getModuleStatus();

How to use ifconfig in your xml you can refer the contact module

<block class="Magento\Framework\View\Element\Html\Link\Current" ifconfig="contact/contact/enabled" name="contact-us-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">Contact Us</argument>
                    <argument name="path" xsi:type="string">contact</argument>
                </arguments>
            </block>

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.