是否可以为 Magento 2 中的模块配置添加 system.xml
文件?如果是,怎么办?
最佳答案
是的,在 magento 2 中可以创建与 Magento 1.x 相同的系统配置文件。但它需要创建一些其他文件。
需要使用下面的文件来创建。
1) app/code/Vendor/Helloworld/etc/adminhtml/system.xml
2) app/code/Vendor/Helloworld/etc/acl.xml
这 2 个文件对于创建系统配置很重要。
在system.xml
文件中
添加通用内容
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../Config/etc/system_file.xsd">
<system>
<!-- Add new Tab -->
<tab id="vendor" translate="label" sortOrder="300">
<label>Vendor Extension</label>
</tab>
<section id="helloworld" translate="label" type="text" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Helloworld</label>
<tab>vendor</tab>
<!-- resource tag name which we have to defined in the acl.xml -->
<resource>Vendor_Helloworld::config_helloworld</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Options</label>
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
</section>
</system>
</config>
在acl.xml
文件中
在文件中需要写入以下内容
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/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">
<!-- this resource id we can use in system.xml for section -->
<resource id="Vendor_Helloworld::config_helloworld" title="Helloworld Section" sortOrder="80" />
</resource>
</resource>
</resource>
</resource>
</resources>
</acl>
</config>
之后,清除 magento 缓存并从管理员端注销。然后在管理员端登录。在商店 > 配置中,您可以看到“供应商扩展”选项卡。当您单击它时,您可以看到它的详细信息。
关于php - Magento 2 : system. 自定义模块中的 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32614392/