添加组

【译者注:按照逻辑,这里应该讲的内容是添加选项。Mageto中,选项是按照组(Group)来划分的,所以我们在添加选项之前得先添加组。】修改system.xml


Location:
 app/
code/
local/
Zhlmmc/
Helloworld/
etc/
system
.
xml
<
config>

<
tabs>

<
helloconfig translate=
"label"
 module=
"helloworld"
>

<
label>
Hello Config</
label>

<
sort_order>
99999
</
sort_order>

</
helloconfig>

</
tabs>
 
<
sections>

<
helloworld_options translate=
"label"
 module=
"helloworld"
>

<
label>
Hello World Config Options</
label>

<
tab>
helloconfig</
tab>

<
frontend_type>
text</
frontend_type>

<
sort_order>
1000
</
sort_order>

<
show_in_default>
1
</
show_in_default>

<
show_in_website>
1
</
show_in_website>

<
show_in_store>
1
</
show_in_store>

<
groups>

<
messages translate=
"label"
>

<
label>
Demo Of Config Fields</
label>

<
frontend_type>
text</
frontend_type>

<
sort_order>
1
</
sort_order>

<
show_in_default>
1
</
show_in_default>

<
show_in_website>
1
</
show_in_website>

<
show_in_store>
1
</
show_in_store>

</
messages>

</
groups>

</
helloworld_options>

</
sections>
     
</
config>



这里也没什么好解释的。刷新一下页面看看你就什么都明白了。

添加配置选项

最后,我们要添加每一个单独的配置选项。配置选项是以节点的形式添加到节点下面的。



<
 !
– … –>

<
messages translate=
"label"
>

<
label>
Demo Of Config Fields</
label>

<
frontend_type>
text</
frontend_type>

<
sort_order>
1
</
sort_order>

<
show_in_default>
1
</
show_in_default>

<
show_in_website>
1
</
show_in_website>

<
show_in_store>
1
</
show_in_store>

<
fields>

<
hello_message>

<
label>
Message</
label>

<
frontend_type>
text</
frontend_type>

<
sort_order>
1
</
sort_order>

<
show_in_default>
1
</
show_in_default>

<
show_in_website>
1
</
show_in_website>

<
show_in_store>
1
</
show_in_store>

</
hello_message>

</
fields>

</
messages>

<
 !
– … –>



这里有一个节点需要说明,“”,刚才说这个节点没什么用。但是这里有用了,这个节点说明了这个选项的数据类型。你 可以把它换成别的类型,比如“time”。这里支持大部分默认的Varien定义的数据类型(lib/Varien/Data/Form /Element)。这个有点像是工厂(Factory)设计模式。让我们把类型改成“select”。你会看到一个下拉框,但是没有选项。我们来添加选 项。首先我们要添加一个源模型(Source Model)



<
hello_message>

<
label>
Message</
label>

<
frontend_type>
select</
frontend_type>

<
 !
– adding a source model –>

<
source_model>
helloworld/
words</
source_model>
                           
<
sort_order>
1
</
sort_order>

<
show_in_default>
1
</
show_in_default>

<
show_in_website>
1
</
show_in_website>

<
show_in_store>
1
</
show_in_store>
                    
</
hello_message>



“”定义了源模型的URI。和我们以前创建的模型一样,源模型也是一个模型,为“select”提供了默认的数据。我想我不说你也明白,根据这里的URI定义,我们要创建以下文件



File
:
 app/
code/
local/
Zhlmmc/
Helloworld/
Model/
Words.
php
class
 Zhlmmc_Helloworld_Model_Words
{

public
 function
 toOptionArray(
)

{

return
 array
(

array
(
'value'
=>
1
,
 'label'
=>
Mage::
helper
(
'helloworld'
)
->
__(
'Hello'
)
)
,

array
(
'value'
=>
2
,
 'label'
=>
Mage::
helper
(
'helloworld'
)
->
__(
'Goodbye'
)
)
,

array
(
'value'
=>
3
,
 'label'
=>
Mage::
helper
(
'helloworld'
)
->
__(
'Yes'
)
)
,

array
(
'value'
=>
4
,
 'label'
=>
Mage::
helper
(
'helloworld'
)
->
__(
'No'
)
)
,

)
;

}

}



源模型提供了一个方法“toOptionsArray”,返回的数据时用来填充我们之前定义的配置选项的。这个方法在运行时会被“initFields”调用。“initFields”在以下类中定义



app/
code/
core/
Mage/
Adminhtml/
Block/
System
/
Config/
Form.
php



我们这里调用了帮助类的翻译函数(__)来获取数据。虽然不是很必要,但调用翻译函数总是一个好习惯。说不定哪天你要将模块翻译成日文呢。【译者 注:值得 注意的是我们这里创建的模型不需要继承任何父类,只需要拥有“toOptionArray”方法就可以了。我觉得这个很不科学,起码要继承一个接口吧】

在已有的配置段或者组中添加数据

除了新建一个标签页,或者配置段,你也可以利用已有的标签页和配置段,向里面添加内容。比如我们添加以下代码到system.xml



File
:
 app/
code/
local/
Zhlmmc/
Helloworld/
etc/
system
.
xml
<
config>

<
 !
– … –>

<
sections>

<
 !
– … –>

<
general>

<
groups>

<
example>

<
label>
Example of Adding a Group</
label>

<
frontend_type>
text</
frontend_type>

<
sort_order>
1
</
sort_order>

<
show_in_default>
1
</
show_in_default>

<
show_in_website>
1
</
show_in_website>

<
show_in_store>
1
</
show_in_store>
                    
</
example>

</
groups>

</
general>

<
 !
– … –>


 
</
sections></
config>



刷新页面,你会在“General”标签页下面看到一个新的组,叫做“Example of Adding a Group”。

如何获得配置数据

到目前为止,我们只是讲了如何设置Magento,可以让用户可以配置我们的模块。现在让我们来看看如何获取用户的配置数据。



Mage::
getStoreConfig
(
'helloworld_options/messages/hello_message'
)
;



上面这行代码就可以获取我们上面配置的那个“select”选项的数据。这个函数的参数是我们要获取的数据的URI,格式如下



section_name/
group_name/
field_name



你也可以通过以下代码来获取一个组或者段的所有值



Mage::
getStoreConfig
(
'helloworld_options/messages'
)
;


 Mage::
getStoreConfig
(
'helloworld_options'
)
;



最后,如果你想获取针对某个特定店面(store)的数据,你可以传入store ID



Mage::
getStoreConfig
(
'helloworld_options'
,
1
)
;



总结

这一章我们讲了如何在Magento的后台管理中添加个性化的配置。我们也顺便介绍了帮助类的使用和ACL基础。这里最重要的内容是后台配置的层级 结构,标签页包含了配置段,配置段包含了组,组包含了配置选项。我们将在以后的章节中介绍系统配置的高级内容,包括自定义格式,数据验证等等。


打开网站     取消