System
System config is used in Magento for storing settings to use in your module.
For example an option to enable and disable your module.
Snippet Instructions:
- Fill in the Tab (can be found in Magento Adminpanel > Stores > Settings > Configuration)
- Check the box to add your config to an existing Tab
- Fill in the Section
- Fill in the Group
- Fill in the Field
- Select the Field type
- Check the box to make your config available in the Graphql StoreConfig endpoint
Available Field Types:
- Text
- Textarea
- Select
- Multiselect
- Encrypted (Obscure)
For Select and Multiselect you will need to define a source model. By default this will be this will be the core Magento yes/no.
Retrieve config value:
To retrieve the value you can use the xml path yourmodulename/general/enabled
$this->_scopeConfig->getValue('yourmodulename/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
(Depends on \Magento\Framework\App\Config\ScopeConfigInterface)
More information:
Retrieve config in GraphQl:
Query a store’s configuation
The following call returns all details of a store’s configuration. .. json:
{ storeConfig { yourmodulename_general_enabled } }
More information
Use the snippet in the Magento 2 module creator.
Files
etc/acl.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Magento_Backend::stores">
<resource id="Magento_Backend::stores_settings">
<resource id="Magento_Config::config">
<resource id="Mage2Gen_Module::config_mage2gen_module" title="Test"/>
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
etc/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>
<Test>
<Test>
<Test/>
</Test>
</Test>
</default>
</config>
etc/adminhtml/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>
<section id="Test" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Test</label>
<tab>Test</tab>
<resource>Mage2Gen_Module::config_mage2gen_module</resource>
<group id="Test" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Test</label>
<field id="Test" type="text" sortOrder="10" showInWebsite="1" showInStore="1" showInDefault="1" translate="label">
<label>Test</label>
<comment/>
</field>
</group>
</section>
</system>
</config>