Sunday, March 18, 2012

增加Magento后台管理功能三:在后台设置(System/Configuration)中加入自定义设置

在模块的etc目录下新建system.xml配置文件,内容如下: 
<?xml version="1.0"?>
<config>
    <sections>
        <!--要加入的section的标识符,此处为sales, 也可以加入自己的section -->
        <sales>
            <groups>
                <my_settings translate="label">
                    <label>Settings 1</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>500</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>0</show_in_website>
                    <show_in_store>0</show_in_store>
                    <fields>
                        <enabled translate="label">
                            <label>Enabled</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </enabled>
                       <settings_2 translate="label">
                      <label>Settings2 </label>
                      <frontend_type>text</frontend_type>
                      <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                     </settings_2 >
                    </fields>
                </my_settings>
            </groups>
        </giftcard>
    </sections>
</config>

可以通过Mage::getStoreConfig('[section 标识符]/[group标识符]/[field标识符]')来获取设置的值。 
Mage::getStoreConfigFlag方法用来返回bool类型的结果,通过用于开关模块的设置。 

如果是自定义section, 则需要在config.xml中加入权限设置: 
<acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <[自定义section标识符]>
                                            <title>[出现在权限列表中的标题]</title>
                                        </[自定义section标识符]>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>


对于新加入的section,需要后台管理用户重新登录后才能生效。

2 comments: