The system.xml is a configuration file which is used to manage the Magento system configuration. After creating this file you can check Store -> Settings -> Configuration
.
File Path : app/code/V4U/Helloworld/etc/adminhtml/system.xml
Step 1 : Create system.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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="v4u" translate="label" sortOrder="10"> <label>V4U</label> </tab> <section id="helloworld" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1"> <class>separator-top</class> <label>Hello World</label> <tab>v4u</tab> <resource>V4U_Helloworld::helloworld_config</resource> <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0"> <label>General Configuration</label> <field id="enable" 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> |
Step 2 : You can set default value of system.xml fields
File Path : app/code/V4U/Helloworld/etc/config.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?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> <helloworld> <general> <enable>0</enable> <display_text>V4U Hello World</display_text> </general> </helloworld> </default> </config> |
Step 3 : Flush the cache : Using Terminal : Run the below command in terminal
php bin/magento cache:flush
or php bin/magento c:f
From Admin Panel : Admin Panel -> System -> Tools -> Cache Management
Step 4 : Get value from configuration
Way 1 : By creating helper
File Path : app/code/V4U/Helloworld/Helper/Data.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?php namespace V4U\Helloworld\Helper; use Magento\Framework\App\Config\ScopeConfigInterface; class Data extends \Magento\Framework\App\Helper\AbstractHelper { /** * Admin configuration paths * */ const IS_ENABLED = 'helloworld/general/enable'; const DISPLAY_TEXT = 'helloworld/general/display_text'; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * Data constructor * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { parent::__construct($context); } /** * @return $isEnabled */ public function isEnabled() { $isEnabled = $this->scopeConfig->getValue(self::IS_ENABLED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); return $isEnabled; } /** * @return $displayText */ public function getDisplayText() { $displayText = $this->scopeConfig->getValue(self::DISPLAY_TEXT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); return $displayText; } } |
Now create block and call helper
File Path : app/code/V4U/Helloworld/Block/Helloworld.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace V4U\Helloworld\Block; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use V4U\Helloworld\Helper\Data; use Magento\Framework\App\Config\ScopeConfigInterface; class Helloworld extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Backend\Block\Template\Context $context, Data $helper, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, array $data = [] ) { $this->helper = $helper; $this->scopeConfig = $scopeConfig; parent::__construct($context, $data); } /* * @return bool */ public function isEnabled() { return $this->helper->isEnabled(); } /* * @return string */ public function getDisplayText() { return $this->helper->getDisplayText(); } } |
Now can call the system config value in PHTML file.
File Path : app/code/V4U/Helloworld/view/frontend/templates/helloworld.phtml
1 2 |
<?= $this->isEnabled(); ?> <?= $this->getDisplayText(); ?> |
Or you can call helper direct in PHTML file.
1 2 3 4 5 6 7 8 |
<?php $helper = $this->helper('V4U\Helloworld\Helper\Data'); $isEnabled = $helper->isEnabled(); $getDisplayText = $helper->getDisplayText(); ?> <?= $isEnabled; ?> <?= $getDisplayText; ?> |
Way 2 : Using dependency
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $_scopeConfig; /** * Admin configuration paths * */ const IS_ENABLED = 'helloworld/general/enable'; public function __construct( ...... \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, ...... ) { ...... $this->_scopeConfig = $scopeConfig; ...... } $isEnabled = $this->_scopeConfig->getValue(self::IS_ENABLED, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); // Add in function. |
Way 3 : Using instantiating a block (in .phtml
file)
1 2 3 4 5 |
<?php $config = $block->getLayout()->createBlock(\Magento\Config\Block\System\Config\Form::class); $isEnabled = $config->getConfigValue('helloworld/general/enable'); ?> <?= $isEnabled; ?> |
Way 4 : Using ObjectManager (It’s not good practice to use directly use ObjectManager. Always use constructor via DI(Dependency injection).Here is just used for concept)
1 2 3 4 5 6 7 |
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES; $isEnabled = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('helloworld/general/enable',$storeScope); ?> <?= $isEnabled; ?> |
Happy Coding. Keep Liking & Sharing