[作者:David Lambauer]{class="badge informative" title="大卫·兰鲍尔"} [仅限PaaS]{class="badge informative" title="仅适用于云项目(Adobe管理的PaaS基础架构)和内部部署项目上的Adobe Commerce 。"}
system.xml引用
system.xml文件允许您管理Commerce系统配置。 使用此主题作为system.xml文件的常规引用。 system.xml文件位于给定Commerce 2扩展名中的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>
<!-- PLACE YOUR MODULE SPECIFIC CONFIGURATION HERE -->
</system>
</config>
bin/magento dev:urn-catalog:generate [--ide IDE] [--] <path>。选项卡//区域//组//字段
在system.xml文件中,可以定义四个相互关联的不同类型的实体。 以下部分介绍选项卡、部分、组和字段之间的关系。 以下屏幕截图显示了管理员后端中的Commerce 2系统配置。
红色方块标记在system.xml文件中定义的不同类型:
选项卡用于在语义上分割不同的配置区域。 每个选项卡可以包含一个或多个部分,这些部分也可以作为子菜单引用。 分区包含一个或多个组。
每个组列出一个或多个字段。 您还可以使用组为以下字段添加一般说明。 如前所述,每个组可以有一个或多个字段。 字段是最小的实体
在系统配置上下文中。
选项卡
<tab> — 标记引用了系统配置中的现有选项卡或新选项卡。
选项卡属性引用
<tab> — 标记可以具有以下属性:
idtypeIdtranslatelabel以使标签可翻译。stringsortOrderfloatclassstring选项卡节点引用
<tab> — 标记可以具有以下子项:
labelstring示例:创建选项卡
以下代码段演示了使用示例数据创建新选项卡的过程。
<?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>
上面的代码片段将创建一个标识符为A_UNIQUE_ID的新选项卡。 由于translate属性已定义并引用了标签,因此label节点是可翻译的。 在渲染过程中,CSS类a-custom-css-class-to-style-this-tab将应用于为此选项卡创建的HTML元素。
值为sortOrder的10属性定义呈现时选项卡在所有选项卡列表中的位置。
区域
<section> — 标记引用了系统配置中的现有节或新节。
章节属性参考
<section> — 标记可以具有以下属性:
idtypeIdtranslatelabel以使标签可翻译。stringtypetext。stringsortOrderfloatshowInDefault1显示节,指定0隐藏节。intshowInStore1显示节,指定0隐藏节。intshowInWebsite1显示节,指定0隐藏节。intcanRestoreintadvancedboolextendsstring节节点引用
<section> — 标记可以具有以下子项:
labelstringclassstringtabtypeTabIdheader_cssstringresourcetypeAclResourceIdgrouptypeGroupfrontend_modeltypeModelincludesystem_include.xsd个兼容文件。 通常用于构造大型system.xml文件。includeType示例:创建部分并将其分配给选项卡
以下代码片段演示了创建新部分的基本用法。
<?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>
上述部分定义了ID A_UNIQUE_SECTION_ID,在默认配置视图和存储上下文中可见。 label节点是可翻译的。 分区与ID为A_UNIQUE_ID的选项卡相关联。 只有在ACL VENDOR_MODULE::path_to_the_acl_resource中定义权限的用户才能访问该部分。
组
<group> — 标记用于将字段组合在一起。
组属性引用
<group> — 标记可以具有以下属性:
idtypeIdtranslatelabel以使标签可翻译。 多个字段应使用空格分隔。stringtypetext。stringsortOrderfloatshowInDefault1显示组,指定0隐藏组。intshowInStore1显示组,指定0隐藏组。intshowInWebsite1显示组,指定0隐藏组。intcanRestoreintadvancedboolextendsstring组节点引用
<group> — 标记可以具有以下子项:
labelstringfieldset_cssstringfrontend_modeltypeModelclone_modeltypeModelclone_fieldsinthelp_urltypeUrlmore_urltypeUrldemo_linktypeUrlcomment<![CDATA[//]]>,可以应用HTML。stringhide_in_single_store_mode1隐藏组;0显示该组。intfieldfieldgroupunboundeddepends1时才用于显示特定字段/组。 此节点需要section/group/field字符串。dependsattributeattributeincludesystem_include.xsd个兼容文件。 通常用于构造大型system.xml文件。includeTypemore_url、demo_url和help_url由只使用一次的PayPal前端模型定义。 这些节点不可重用。示例:为给定节创建组
以下代码段演示了创建新组的基本用法。
<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>
上述组定义ID A_UNIQUE_GROUP_ID,在默认配置视图和存储上下文中可见。 label和comment都标记为可翻译。
字段
<field> — 标记用在<group> — 标记内部以定义特定的配置值。
字段属性引用
<field> — 标记可以具有以下属性:
idtypeIdtranslatelabel以使标签可翻译。 多个字段应使用空格分隔。stringtypetext。stringsortOrderfloatshowInDefault1显示字段,指定0隐藏字段。intshowInStore1显示字段,指定0隐藏字段。intshowInWebsite1显示字段,指定0隐藏字段。intcanRestoreintadvancedboolextendsstring字段类型引用
<field> — 标记的type=""属性可以具有以下值:
texttextareaselectsource_model。 也用于Yes/No选择。 有关示例,请参阅Magento\Search\Model\Adminhtml\System\Config\Source\Engine。multiselectselect,但多个选项有效。buttonMagento\ScheduledImportExport\Block\Adminhtml\System\Config\Clean。obscure**​**。 在浏览器中使用“检查元素”更改类型不会显示值。passwordobscure类似,只是隐藏值未加密,在浏览器中使用“Inspect Element”强制更改类型确实会显示该值。filelabeltimeallowspecificsource_model,例如Magento\Shipping\Model\Config\Source\Allspecificcountriesimagenotefrontend_model才能渲染注释。也可以创建自定义字段类型。 这通常在需要带有操作的特殊按钮时完成。 要实现此目的,需要两个主要元素:
- 在
adminhtml区域中创建块 - 正在将
type=""设置为此块的路径
块本身至少需要__construct方法和getElementHtml()方法。 Magento_OfflineShipping是自定义类型的简单示例。
例如,在OfflineShipping模块中, Export按钮在Magento\OfflineShipping\Block\Adminhtml\Form\Field\Export中定义,字段定义如下所示:
<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> — 标记可以具有以下子项:
labelstringcomment<![CDATA[//]]>,可以应用HTML。stringtooltipstringhintfrontend_model可用。stringfrontend_classstringfrontend_modeltypeModelbackend_modeltypeModelsource_modeltypeModelconfig_pathtypeConfigPathvalidatestringcan_be_emptytype为multiselect时使用,以指定字段可以为空。intif_module_enabledtypeModulebase_urlupload_dir结合使用进行文件上载。typeUrlupload_dirtypeUploadDirbutton_urlbutton_url和button_label,则显示按钮。 通常与自定义前端模型结合使用。typeUrlbutton_labelbutton_label和button_url,则显示按钮。 通常与自定义前端模型结合使用。stringmore_urltypeUrldemo_urltypeUrlhide_in_single_store_mode1隐藏组;0显示该组。intoptionscomplexTypedepends1时仅显示特定字段/组。 此节点需要section/group/field字符串。complexTypeattributecomplexTyperequirescomplexTypemore_url、demo_url、requires和options由不同的核心付款模型定义,并且只使用一次。 这些节点不可重用。示例:在给定组中创建两个字段
<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>
上面的示例创建了两个字段,它们在默认视图和存储视图中均可见/可配置。 这两个字段均具有注释和工具提示以向用户描述其用途。 label节点是可翻译的。
全局启用ANOTHER_UNIQUE_FIELD_ID中的给定模块时,标识符为if_module_enabled的字段可见。 该字段还根据规则required-entry和no-whitespace验证其值。
标识符为A_UNIQUE_FIELD_ID的字段定义了一个不同的源模型,它提供该值Yes和No。
通用源模型
Commerce 2 Core提供了以下源模型。 一般来说,源模型要多得多;以下列表描述了最常见的模型。 请注意,这些源模型需要将字段属性type设置为select才能正常工作。
Magento\Config\Model\Config\Source\YesnocustomYes、No和Specified。Magento\Config\Model\Config\Source\EnabledisableEnable、Disable。 在数据库中将值保存为0和1。Magento\AdminNotification\Model\Config\Source\Frequency1 Hour、2 Hours、6 Hours、12 Hours和24 Hours。 值将另存为整数。Magento\Catalog\Model\Config\Source\TimeFormatMagento\Cron\Model\Config\Source\FrequencyDaily、Weekly和Monthly。 值在数据库中保存为D、W和M。Magento\GoogleAdwords\Model\Config\Source\LanguageMagento\Config\Model\Config\Source\Locale字段验证
字段可以分配一个或多个验证器类,以确保用户的输入符合扩展的要求。 可以使用<validate>标记应用验证规则。
以下示例验证一个字段并添加多个不同的验证规则。
<field id="A_CUSTOM_IDENTIFIER" showInDefault="1" showInWebsite="0" showInStore="1">
<validate>required-entry validate-clean-url no-whitespace</validate>
</field>
可以使用以下验证规则:
alphanumericintegeripv4ipv6letters-onlyabcABC。letters-with-basic-punc必须传递以下表达式:
/^[a-z\-.,()\u0027\u0022\s]+$/i。mobileUKno-marginal-whitespaceno-whitespacephoneUKphoneUSrequired-entryvalidate-no-empty)。验证失败消息:“这是必填字段。”
time15、15:05或15:05:48。time12h3 am、11:30 pm、02:15:00 pm。validate-admin-passwordvalidate-alphanum-with-spacesvalidate-clean-urlhttps://www.example.com或www.example.com。validate-currency-dollarvalidate-data第一个字符必须是字母。
(必须匹配表达式:
/^[A-Za-z]+[A-Za-z0-9_]+$/)验证失败消息:“请仅在此字段中使用字母(a-z或A-Z)、数字(0-9)或下划线(_),第一个字符应为字母。”
validate-date-auvalidate-emailvalidate-emailSendervalidate-faxvalidate-no-emptyrequried-entry)。验证失败消息:“空值。”
validate-no-html-tagsvalidate-passwordvalidate-phoneLaxvalidate-phoneStrictvalidate-selectnull值、字符串值none或字符串长度0。validate-ssnvalidate-streetvalidate-urlvalidate-xml-identifiervalidate-zip-usvinUS默认值
可以通过在etc/config.xml节点中指定默认值,在模块的section/group/field_ID文件中设置字段的默认值。
示例:设置ANOTHER_UNIQUE_FIELD_ID的默认值(默认范围)
<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>
示例:设置ANOTHER_UNIQUE_FIELD_ID的默认值(网站范围)
使用websites标记指定特定网站的默认值。
<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>