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.

Generated configuration can be found in Magento Adminpanel > Stores > Settings > Configuration

To retrieve the value you can use the xml path yourmodulename/general/enabled

Example:

$this->_scopeConfig->getValue('yourmodulename/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

(Depends on MagentoFrameworkAppConfigScopeConfigInterface)

Field Types:

  • Select
  • Multiselect
  • Text
  • Textarea

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.

Use the System 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" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
			<label>Test</label>
			<tab>Test</tab>
			<resource>Mage2Gen_Module::config_mage2gen_module</resource>
			<group id="Test" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
				<label>Test</label>
				<field id="Test" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="text">
					<label>Test</label>
					<comment/>
				</field>
			</group>
		</section>
	</system>
</config>