Magento 模块开发之 System.xml 配置详解

system.xml 和 config.xml 比较相似, 都位于模块 etc 文件夹中, 且都是用来进行配置, 但其主要用于后台 System -> Configuration 中创建配置信息
为了能更好的理解 system.xml 的功能和显示位置, 请认真看一下如下结构图, 这个页面被分成了几块: 标签(Tab), 区域(Section),组(Group),域(Field)

文本域 (Field)

我们先从基础开始, 首先简单的在 ‘system -> Configuration -> Customer Configuration -> Create New Account Options’ Group 里创建两个文本域

<?xml version="1.0"?>
<config>
<sections> <!-- 区域(Section) 标签 -->
<customer translate="label" module="test"> <!-- 区域名: 声明你想放在哪个区域里 -->
<groups> <!-- 组(Group) 标签 -->
<create_account translate="label"> <!-- 组名: 声明你想放在哪个组里 -->
<fields> <!-- 文本域(field) 标签 -->
<patient translate="label"> <!-- 文本域标识符 -->
<label>Sign In Gate Email Template</label> <!-- 该文本域的 Label 标签内容 -->
<frontend_type>select</frontend_type> <!-- 该文本域 input 的类型 -->
<source_model>adminhtml/system_config_source_email_template</source_model> <!-- 后台邮件模板类 -->
<sort_order>3</sort_order> <!-- 设置该 field 在 group 内的排序 -->
<show_in_default>1</show_in_default> <!-- 是否在默认的 store 显示 -->
<show_in_website>1</show_in_website> <!-- 是否在 website 显示 -->
<show_in_store>1</show_in_store> <!-- 是否在 store 显示 -->
</patient>
</fields>
<fields> <!-- 另外一个文本域 -->
<slider translate="label">
<label>Show Slider In Sign In Gate</label>
<frontend_type>text</frontend_type> <!-- input 的类型为 text -->
<sort_order>99</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</slider>
</fields>
</create_account>
</groups>
</customer>
</sections>
</config>

显示的效果:

如果你想获取到这些文本域的值, 你可以在模块里使用方法: Mage::getStoreConfig(‘section_key/group_key/field_key’,Mage::app()->getStore());
以如上为例:

Mage::getStoreConfig(‘customer/create_account/slider’,Mage::app()->getStore());

当在后台填写保存后, Magento 将这些值都存在 core_config_data 表中, 如果你看下表结构, 就可以很清楚的看到 scope, path 和 value 等字段, path列就是存储文本域的路径, 也就是 Mage::getStoreConfig() 方法中的参数一
组 (Group)
这里,我们再来创建一个新的 Group, 代码如下:

<?xml version="1.0"?>
<config>
<sections>
<customer translate="label" module="test">
<groups>
<test translate="label"> <!-- 新组(group)的名/标识符(Key)-->
<label>Test Group</label> <!-- label 内容 -->
<sort_order>10</sort_order> <!-- group 排序 -->
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<patient translate="label">
<label>Sign In Gate Email Template</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</patient>
</fields>
<fields>
<slider translate="label">
<label>Show Slider In Sign In Gate</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>99</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</slider>
</fields>
</test>
</groups>
</customer>
</sections>
</config>

输出如下效果:

区域 (Section)

这里,我们来创建一个新的 Section, 代码如下:

<?xml version="1.0"?>
<config>
<sections>
<testsection translate="label" module="test">
<label>New Test Section</label>
<tab>customer</tab>
<sort_order>130</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<test translate="label">
<label>Test Group</label>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<patient translate="label">
<label>Sign In Gate Email Template</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_email_template</source_model>
<sort_order>3</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</patient>
</fields>
<fields>
<slider translate="label">
<label>Show Slider In Sign In Gate</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>99</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</slider>
</fields>
</test>
</groups>
</testsection>
</sections>
</config>

完成后, 你应该可以在 Customer Tab 里看到 “New Test Section”, 但还需要一步, 那就是要在 config.xml 里添加权限(ACL), 不然你点击就会发现 404 错误, 你可以看下类: Mage_Adminhtml_System_ConfigController, 在 259 行左右的 _isSectionAllowed($section) 方法, 它会检查新的 seciton 是否有权限, 如果有才会显示出来

现在在 config.xml 里的 <adminhtml> 标签中写入如下代码:








  <acl><resources><admin><!-- acl 是基于 URL 的. 这里你可以看到 URL 是 /admin/system_config/ --><children><system><children><config><children><testsectiontranslate="title"module="test"><!-- 这里就是我们创建的 section 名称 -->
< title >Test Section ACL</ title > <!-- 这个 title 将显示在 User -> Roles -> Permissions --> < sort_order >99</ sort_order > </ testsection > </ children > </ config > </ children > </ system > </ children > </ admin > </ resources > </ acl >

现在你登出后台, 重新登入, 新的 section 已经工作正常了

标签 (Tab)

创建一个新的 tab, 代码如下:

 


  





  








 






 










 













<?xmlversion="1.0"?><config><tabs><newtabtranslate="label"module="customer"><label>New Tab</label><sort_order>1</sort_order></newtab></tabs><sections><testsectiontranslate="label"module="test"><class>separator-top</class><label>New Test Section</label><tab>newtab</tab><sort_order>130</sort_order><show_in_default>1</show_in_default><show_in_website>1</show_in_website><show_in_store>1</show_in_store><groups><testtranslate="label"><label>Test Group</label><sort_order>10</sort_order><show_in_default>1</show_in_default><show_in_website>1</show_in_website><show_in_store>1</show_in_store><fields><patienttranslate="label"><label>Sign In Gate Email Template</label><frontend_type>select</frontend_type><source_model>adminhtml/system_config_source_email_template</source_model><sort_order>3</sort_order><show_in_default>1</show_in_default><show_in_website>1</show_in_website><show_in_store>1</show_in_store></patient></fields><fields><slidertranslate="label"><label>Show Slider In Sign In Gate</label><frontend_type>select</frontend_type><source_model>adminhtml/system_config_source_yesno</source_model><sort_order>99</sort_order><show_in_default>1</show_in_default><show_in_website>1</show_in_website><show_in_store>1</show_in_store></slider></fields></test></groups></testsection></sections></config>

同样, 此操作也需要之前的 ACL 配置, 有些情况下会报 permission 错误, 你可以到 System -> Permission ->Roles, 点击你的角色编辑再保存

为新建的文本域添加默认值

在 system.xml 中完成如上操作后, 我们也可以为其添加默认值, 比如说有一个 Yes/No 下拉框, 你可以在 config.xml 中添加默认值:








<default><!-- 需要直接写在 <config> 标签里 --><testsection><!-- section 名 --><test><!-- group 名--><patient>1</patient><!-- field 名和值--><slider>1</slider></test></testsection></default>

到这里就了解了 system.xml 的基础, 在随后的章节里, 我会列出不同种类的文本域, 方便大家以后使用时可以直接查阅调用

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念