Site icon Qaisar Satti's Blogs

Magento 2 create retrieve system configuration

Magento 2 create retrieve system configuration

Magento 2 create retrieve system configuration

Today we talk about how in Magento 2 create retrieve system configuration. This tutorial includes the knowledge of get value of system configuration, how to add your custom configurations in system configurations and also how to retrieve their values too. Configuration values are saved in core_config_data table , in this table you can see the path and value of your configuration settings. So let’s start with our example.

Create system.xml

First step is create system.xml in following directory.

QaisarSatti/HelloWorld/etc/adminhtml/

<?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="qaisarsatti_helloworld" translate="label" sortOrder="200">
                <label>QaisarSatti HelloWorld</label>
            </tab>
        <section id="helloworld" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>HelloWorld COnfiguration</label>
            <tab>qaisarsatti_helloworld</tab>
            <resource>qaisarsatti_helloworld::config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                <field id="enabled" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enabeld </label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="title" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Title</label>
                </field>                                            
            </group>
        </section>
    </system>
</config>

Retrieve System configuration

For retrieving the values we create the helper file Data.php in following directory

QaisarSatti/HelloWorld/Helper/Data.php

First thing you need to know for retrieving the values in is combination of sestionid/groupid/fieldid to get value.

<?php

namespace QaisarSatti\HelloWorld\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    const HELLOWORLD_MODULE_ENABLED    = 'helloworld/general/enabled';
    const HELLOWORLD_LABEL             = 'helloworld/general/title';



    public function __construct(
        \Magento\Framework\App\Helper\Context $context
    ) {
   
   
   
   
        parent::__construct($context);
    }
    public function getEnabled()
    {
        return $this->scopeConfig->getValue(
            self::HELLOWORLD_MODULE_ENABLED,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
    public function getLabel()
    {
        return$this->scopeConfig->getValue(
            self::HELLOWORLD_LABEL,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Now: Following you can retrieve system configuration.

Exit mobile version