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:

  1. Fill in the Tab (can be found in Magento Adminpanel > Stores > Settings > Configuration)
  2. Check the box to add your config to an existing Tab
  3. Fill in the Section
  4. Fill in the Group
  5. Fill in the Field
  6. Select the Field type
  7. 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:

https://github.com/magento/magento2/blob/2.3-develop/lib/internal/Magento/Framework/App/Config/ScopeConfigInterface.php#L29

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

https://devdocs.magento.com/guides/v2.3/graphql/reference/store-config.html#extend-configuration-data

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>