[Contributed by David Lambauer]{class="badge informative" title="David Lambauer"} [PaaS only]{class="badge informative" title="Applies to Adobe Commerce on Cloud projects (Adobe-managed PaaS infrastructure) and on-premises projects only."}
system.xml reference
The system.xml file allows you to manage the Commerce system configuration. Use this topic as a general reference for the system.xml file. The system.xml file is located under etc/adminhtml/system.xml in a given Commerce 2 extension.
The following code snippet shows the bare skeleton of the file:
<?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>
<!-- PLACE YOUR MODULE SPECIFIC CONFIGURATION HERE -->
</system>
</config>
bin/magento dev:urn-catalog:generate [--ide IDE] [--] <path>.Tabs // Sections // Groups // Fields
In the system.xml file, it is possible to define four different types of entities, which are related to each other. The following section describes the relationship between tabs, sections, groups, and fields. The following screenshot displays the Commerce 2 System Configuration in the Admin backend.
The red squares mark the different types that are defined in the system.xml file:
Tabs are used to split different configuration areas semantically. Each tab can contain one or more sections, which can also be referenced as submenus. A section contains one or more groups.
Each group lists one or more fields. You can also use a group to add a general description for the following fields. As mentioned, each group can have one or more fields. Fields are the smallest entity
in the system configuration context.
Tabs
A <tab>-Tag references to either an existing or a new tab in the system configuration.
Tab attribute reference
A <tab>-Tag can have the following attributes:
idtypeIdtranslatelabel to make the label translatable.stringsortOrderfloatclassstringTab node reference
A <tab>-Tag can have the following child:
labelstringExample: Create a tab
The following code snippet demonstrates the creation of a new tab with example data.
<?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="A_UNIQUE_ID" translate="label" class="a-custom-css-class-to-style-this-tab" sortOrder="10">
<label>A meaningful label</label>
</tab>
</system>
</config>
The snippet above creates a new tab with the identifier A_UNIQUE_ID. As the translate-attribute is defined and references the label, the label-node is translatable. During the rendering process, the CSS class a-custom-css-class-to-style-this-tab will be applied on the HTML element that was created for this tab.
The sortOrder-attribute with the value of 10 defines the position of the tab in the list of all tabs when rendered.
Sections
A <section>-Tag references to either an existing or a new section in the system configuration.
Section attribute reference
A <section>-Tag can have the following attributes:
idtypeIdtranslatelabel to make the label translatable.stringtypetext.stringsortOrderfloatshowInDefault1 to show the section and 0 to hide the section.intshowInStore1 to show the section and 0 to hide the section.intshowInWebsite1 to show the section and 0 to hide the section.intcanRestoreintadvancedboolextendsstringSection node reference
A <section>-Tag can have the following children:
labelstringclassstringtabtypeTabIdheader_cssstringresourcetypeAclResourceIdgrouptypeGroupfrontend_modeltypeModelincludesystem_include.xsd compatible files. Usually used to structure large system.xml files.includeTypeExample: Create a section and assign it to a tab
The following code snippet demonstrates the basic usage of creating a new section.
<?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="A_UNIQUE_ID" translate="label" class="a-custom-css-class-to-style-this-tab" sortOrder="10">
<label>A meaningful label</label>
</tab>
<section id="A_UNIQUE_SECTION_ID" showInDefault="1" showInWebsite="0" showInStore="1" sortOrder="10" translate="label">
<label>A meaningful section label</label>
<tab>A_UNIQUE_ID</tab>
<resource>VENDOR_MODULE::path_to_the_acl_resource</resource>
</section>
</system>
</config>
The section described above defines the ID A_UNIQUE_SECTION_ID, is visible in the default config view and in a store context. The label-node is translatable. The section is associated to the tab with the ID A_UNIQUE_ID. The section can only be accessed by users that have the permissions defined in the ACL VENDOR_MODULE::path_to_the_acl_resource.
Groups
The <group>-Tag is used to group fields together.
Group attribute reference
A <group>-Tag can have the following attributes:
idtypeIdtranslatelabel to make the label translatable. Multiple fields should be separated by a space.stringtypetext.stringsortOrderfloatshowInDefault1 to show the group and 0 to hide the group.intshowInStore1 to show the group and 0 to hide the group.intshowInWebsite1 to show the group and 0 to hide the group.intcanRestoreintadvancedboolextendsstringGroup node reference
A <group>-Tag can have the following children:
labelstringfieldset_cssstringfrontend_modeltypeModelclone_modeltypeModelclone_fieldsinthelp_urltypeUrlmore_urltypeUrldemo_linktypeUrlcomment<![CDATA[//]]> HTML can be applied.stringhide_in_single_store_mode1 hides the group; 0 shows the group.intfieldfieldgroupunboundeddepends1. This node expects a section/group/field-string.dependsattributeattributeincludesystem_include.xsd compatible files. Usually used to structure large system.xml files.includeTypemore_url, demo_url and help_url are defined by a PayPal frontend model that is only used once. These nodes are not reusable.Example: Create a group for a given section
The following code snippet demonstrates the basic usage of creating a new group.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="A_UNIQUE_ID" translate="label" class="a-custom-css-class-to-style-this-tab" sortOrder="10">
<label>A meaningful label</label>
</tab>
<section id="A_UNIQUE_SECTION_ID" showInDefault="1" showInWebsite="0" showInStore="1" sortOrder="10" translate="label">
<label>A meaningful section label</label>
<tab>A_UNIQUE_ID</tab>
<resource>VENDOR_MODULE::path_to_the_acl_resource</resource>
<group id="A_UNIQUE_GROUP_ID" translate="label comment" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="1">
<label>A meaningful group label</label>
<comment>An additional comment helping users to understand the effect when configuring the fields defined in this group.</comment>
<!-- Add your fields here. -->
</group>
</section>
</system>
</config>
The group described above defines the ID A_UNIQUE_GROUP_ID, is visible in the default config view and in a store context. Both, the label and the comment are marked as translatable.
Fields
The <field>-Tag is used inside of <group>-Tags to define specific configuration values.
Field attribute reference
A <field>-Tag can have the following attributes:
idtypeIdtranslatelabel to make the label translatable. Multiple fields should be separated by a space.stringtypetext.stringsortOrderfloatshowInDefault1 to show the field and 0 to hide the field.intshowInStore1 to show the field and 0 to hide the field.intshowInWebsite1 to show the field and 0 to hide the field.intcanRestoreintadvancedboolextendsstringField type reference
A <field>-Tag can have the following values for the type="" attribute:
texttextareaselectsource_model. Also used for Yes/No selections. See Magento\Search\Model\Adminhtml\System\Config\Source\Engine for an example.multiselectselect but multiple options are valid.buttonMagento\ScheduledImportExport\Block\Adminhtml\System\Config\Clean for an example.obscure****. Changing the type using “Inspect Element” in the browser does not reveal the value.passwordobscure except that the hidden value is not encrypted, and forcibly changing the type using “Inspect Element” in the browser does reveal the value.filelabeltimeallowspecificsource_model such as Magento\Shipping\Model\Config\Source\Allspecificcountriesimagenotefrontend_model to render the note.It is also possible to create a custom field type. This is often done when a special button, with an action, is required. To do this requires two main elements:
- Creating a block in the
adminhtmlarea - Setting the
type=""to the path to this block
The block itself requires, at a minimum, a __construct method and a getElementHtml() method. The Magento_OfflineShipping is a simple example of a custom type.
For example, in the OfflineShipping module, the Export button is defined in Magento\OfflineShipping\Block\Adminhtml\Form\Field\Export and the field definition looks like:
<field id="export" translate="label" type="Magento\OfflineShipping\Block\Adminhtml\Form\Field\Export" sortOrder="5" showInDefault="0" showInWebsite="1" showInStore="0">
<label>Export</label>
</field>
Field node reference
A <field>-Tag can have the following children:
labelstringcomment<![CDATA[//]]> HTML can be applied.stringtooltipstringhintfrontend_model.stringfrontend_classstringfrontend_modeltypeModelbackend_modeltypeModelsource_modeltypeModelconfig_pathtypeConfigPathvalidatestringcan_be_emptytype is multiselect to specify that a field can be empty.intif_module_enabledtypeModulebase_urlupload_dir for file uploads.typeUrlupload_dirtypeUploadDirbutton_urlbutton_url and button_label are specified. Usually used in combination with a custom frontend model.typeUrlbutton_labelbutton_label and button_url are specified. Usually used in combination with a custom frontend model.stringmore_urltypeUrldemo_urltypeUrlhide_in_single_store_mode1 hides the group; 0 shows the group.intoptionscomplexTypedepends1. This node expects a section/group/field-string.complexTypeattributecomplexTyperequirescomplexTypemore_url, demo_url, requires and options are defined by a different core payment model and are only used once. These nodes are not reusable.Example: Create two fields in a given group
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="A_UNIQUE_ID" translate="label" class="a-custom-css-class-to-style-this-tab" sortOrder="10">
<label>A meaningful label</label>
</tab>
<section id="A_UNIQUE_SECTION_ID" showInDefault="1" showInWebsite="0" showInStore="1" sortOrder="10" translate="label">
<label>A meaningful section label</label>
<tab>A_UNIQUE_ID</tab>
<resource>VENDOR_MODULE::path_to_the_acl_resource</resource>
<group id="A_UNIQUE_GROUP_ID" translate="label" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="1">
<label>A meaningful group label</label>
<comment>An additional comment helping users to understand the effect when configuring the fields defined in this group.</comment>
<field id="A_UNIQUE_FIELD_ID" translate="label" sortOrder="10" showInDefault="0" showInWebsite="0" showInStore="1" type="select">
<label>Feature Flag Example</label>
<comment>This field is an example for a basic yes or no select.</comment>
<tooltip>Usually these kinds of fields are used to enable or disable a given feature. Other fields might be dependent to this and will only appear if this field is set to yes.</tooltip>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="ANOTHER_UNIQUE_FIELD_ID" translate="label" sortOrder="10" showInDefault="0" showInWebsite="0" showInStore="1" type="text">
<label>A meaningful field label</label>
<comment>A descriptive text explaining this configuration field.</comment>
<tooltip>Another possible frontend element that also can be used to describe the meaning of this field. Will be displayed as a small icon beside the field.</tooltip>
<validate>required-entry no-whitespace</validate> <!-- Field is required and must not contain any whitespace. -->
<if_module_enabled>VENDOR_MODULE</if_module_enabled>
<depends> <!-- This field will only be visible if the field with the id A_UNIQUE_FIELD_ID is set to value 1 -->
<field id="A_UNIQUE_FIELD_ID">1</field>
</depends>
</field>
</group>
</section>
</system>
</config>
The example above creates two fields, both visible/configurable in default and in store view. Both fields have a comment and a tooltip to describe their purpose to the user. The label-node is translatable.
The field with the identifier ANOTHER_UNIQUE_FIELD_ID is visible when the given module in the if_module_enabled is enabled globally. The field also validates its value against the rules required-entry and no-whitespace.
The field with the identifier A_UNIQUE_FIELD_ID defines a different source model which provides tha values Yes and No.
Common source models
The following source models are provided by the Commerce 2 Core. In general, there are many more source models; the following list describes the most common ones. Be aware, that these source models need the field attribute type to be set to select in order to work properly.
Magento\Config\Model\Config\Source\YesnocustomYes, No and Specified.Magento\Config\Model\Config\Source\EnabledisableEnable, Disable. Saves the values as 0 and 1 in the database.Magento\AdminNotification\Model\Config\Source\Frequency1 Hour,2 Hours,6 Hours,12 Hours and 24 Hours. Values are saved as integers.Magento\Catalog\Model\Config\Source\TimeFormatMagento\Cron\Model\Config\Source\FrequencyDaily, Weekly and Monthly. Values are saved in the database as D, W and M.Magento\GoogleAdwords\Model\Config\Source\LanguageMagento\Config\Model\Config\Source\LocaleField Validation
A field can have one or more validator-classes assigned to make sure that the input of the user meets the requirements of the extension. Validation rules can be applied with the <validate>-Tag.
The following example validates a field and adds several different validation rules.
<field id="A_CUSTOM_IDENTIFIER" showInDefault="1" showInWebsite="0" showInStore="1">
<validate>required-entry validate-clean-url no-whitespace</validate>
</field>
The following validation rules are available:
alphanumericintegeripv4ipv6letters-onlyabcABC.letters-with-basic-puncMust pass the following expression:
/^[a-z\-.,()\u0027\u0022\s]+$/i.mobileUKno-marginal-whitespaceno-whitespacephoneUKphoneUSrequired-entryvalidate-no-empty).Validation failure message: “This is a required field.”
time15, 15:05 or 15:05:48.time12h3 am, 11:30 pm, 02:15:00 pm.validate-admin-passwordvalidate-alphanum-with-spacesvalidate-clean-urlhttps://www.example.com or www.example.com.validate-currency-dollarvalidate-dataThe first character must be a letter.
(Must match expression:
/^[A-Za-z]+[A-Za-z0-9_]+$/)Validation failure message: “Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field, and the first character should be a letter.”
validate-date-auvalidate-emailvalidate-emailSendervalidate-faxvalidate-no-emptyrequried-entry).Validation failure message: “Empty value.”
validate-no-html-tagsvalidate-passwordvalidate-phoneLaxvalidate-phoneStrictvalidate-selectnull value, string value of none or string length of 0.validate-ssnvalidate-streetvalidate-urlvalidate-xml-identifiervalidate-zip-usvinUSDefault Values
Default values for fields may be set in the module’s etc/config.xml file by specifying the default value in the section/group/field_ID node.
Example: Setting the default value for ANOTHER_UNIQUE_FIELD_ID (Default scope)
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<A_UNIQUE_SECTION_ID>
<A_UNIQUE_GROUP_ID>
<ANOTHER_UNIQUE_FIELD_ID>This is the default value</ANOTHER_UNIQUE_FIELD_ID>
</A_UNIQUE_GROUP_ID>
</A_UNIQUE_SECTION_ID>
</default>
</config>
Example: Setting the default value for ANOTHER_UNIQUE_FIELD_ID (Website scope)
Using the websites tag, specify the default value for a specific website.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<websites>
<WEBSITE_CODE>
<A_UNIQUE_SECTION_ID>
<A_UNIQUE_GROUP_ID>
<ANOTHER_UNIQUE_FIELD_ID>This is the default value</ANOTHER_UNIQUE_FIELD_ID>
</A_UNIQUE_GROUP_ID>
</A_UNIQUE_SECTION_ID>
</WEBSITE_CODE>
</websites>
</config>