[Contributed by David Lambauer]{class="badge informative" title="David Lambauer"}
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:
id
typeId
translate
label
to make the label translatable.string
sortOrder
float
class
string
Tab node reference
A <tab>
-Tag can have the following child:
label
string
Example: 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:
id
typeId
translate
label
to make the label translatable.string
type
text
.string
sortOrder
float
showInDefault
1
to show the section and 0
to hide the section.int
showInStore
1
to show the section and 0
to hide the section.int
showInWebsite
1
to show the section and 0
to hide the section.int
canRestore
int
advanced
bool
extends
string
Section node reference
A <section>
-Tag can have the following children:
label
string
class
string
tab
typeTabId
header_css
string
resource
typeAclResourceId
group
typeGroup
frontend_model
typeModel
include
system_include.xsd
compatible files. Usually used to structure large system.xml
files.includeType
Example: 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:
id
typeId
translate
label
to make the label translatable. Multiple fields should be separated by a space.string
type
text
.string
sortOrder
float
showInDefault
1
to show the group and 0
to hide the group.int
showInStore
1
to show the group and 0
to hide the group.int
showInWebsite
1
to show the group and 0
to hide the group.int
canRestore
int
advanced
bool
extends
string
Group node reference
A <group>
-Tag can have the following children:
label
string
fieldset_css
string
frontend_model
typeModel
clone_model
typeModel
clone_fields
int
help_url
typeUrl
more_url
typeUrl
demo_link
typeUrl
comment
<![CDATA[//]]>
HTML can be applied.string
hide_in_single_store_mode
1
hides the group; 0
shows the group.int
field
field
group
unbounded
depends
1
. This node expects a section/group/field
-string.depends
attribute
attribute
include
system_include.xsd
compatible files. Usually used to structure large system.xml
files.includeType
more_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:
id
typeId
translate
label
to make the label translatable. Multiple fields should be separated by a space.string
type
text
.string
sortOrder
float
showInDefault
1
to show the field and 0
to hide the field.int
showInStore
1
to show the field and 0
to hide the field.int
showInWebsite
1
to show the field and 0
to hide the field.int
canRestore
int
advanced
bool
extends
string
Field type reference
A <field>
-Tag can have the following values for the type=""
attribute:
text
textarea
select
source_model
. Also used for Yes/No
selections. See Magento\Search\Model\Adminhtml\System\Config\Source\Engine
for an example.multiselect
select
but multiple options are valid.button
Magento\ScheduledImportExport\Block\Adminhtml\System\Config\Clean
for an example.obscure
****
. Changing the type using “Inspect Element” in the browser does not reveal the value.password
obscure
except that the hidden value is not encrypted, and forcibly changing the type using “Inspect Element” in the browser does reveal the value.file
label
time
allowspecific
source_model
such as Magento\Shipping\Model\Config\Source\Allspecificcountries
image
note
frontend_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
adminhtml
area - 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:
label
string
comment
<![CDATA[//]]>
HTML can be applied.string
tooltip
string
hint
frontend_model
.string
frontend_class
string
frontend_model
typeModel
backend_model
typeModel
source_model
typeModel
config_path
typeConfigPath
validate
string
can_be_empty
type
is multiselect
to specify that a field can be empty.int
if_module_enabled
typeModule
base_url
upload_dir
for file uploads.typeUrl
upload_dir
typeUploadDir
button_url
button_url
and button_label
are specified. Usually used in combination with a custom frontend model.typeUrl
button_label
button_label
and button_url
are specified. Usually used in combination with a custom frontend model.string
more_url
typeUrl
demo_url
typeUrl
hide_in_single_store_mode
1
hides the group; 0
shows the group.int
options
complexType
depends
1
. This node expects a section/group/field
-string.complexType
attribute
complexType
requires
complexType
more_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\Yesnocustom
Yes
, No
and Specified
.Magento\Config\Model\Config\Source\Enabledisable
Enable
, Disable
. Saves the values as 0
and 1
in the database.Magento\AdminNotification\Model\Config\Source\Frequency
1 Hour
,2 Hours
,6 Hours
,12 Hours
and 24 Hours
. Values are saved as integers.Magento\Catalog\Model\Config\Source\TimeFormat
Magento\Cron\Model\Config\Source\Frequency
Daily
, Weekly
and Monthly
. Values are saved in the database as D
, W
and M
.Magento\GoogleAdwords\Model\Config\Source\Language
Magento\Config\Model\Config\Source\Locale
Field 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:
alphanumeric
integer
ipv4
ipv6
letters-only
abcABC
.letters-with-basic-punc
Must pass the following expression:
/^[a-z\-.,()\u0027\u0022\s]+$/i
.mobileUK
no-marginal-whitespace
no-whitespace
phoneUK
phoneUS
required-entry
validate-no-empty
).Validation failure message: “This is a required field.”
time
15
, 15:05
or 15:05:48
.time12h
3 am
, 11:30 pm
, 02:15:00 pm
.validate-admin-password
validate-alphanum-with-spaces
validate-clean-url
https://www.example.com
or www.example.com
.validate-currency-dollar
validate-data
The 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-au
validate-email
validate-emailSender
validate-fax
validate-no-empty
requried-entry
).Validation failure message: “Empty value.”
validate-no-html-tags
validate-password
validate-phoneLax
validate-phoneStrict
validate-select
null
value, string value of none
or string length of 0.validate-ssn
validate-street
validate-url
validate-xml-identifier
validate-zip-us
vinUS
Default 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>