magento的configuration扩展

首先,我将创建一个名为“CustomConfig”的基本自定义模块,该模块在System> Configuration 菜单下提供后端配置选项。以下是所需设置所需的文件列表:

app/etc/modules/Envato_All.xml:这是用于启用我们的自定义模块的文件。
 
app/code/local/Envato/CustomConfig/etc/config.xml:这是一个模块配置文件。

app/code/local/Envato/CustomConfig/etc/system.xml:这是一个声明我们模块的配置选项的文件。

app/code/local/Envato/CustomConfig/Model/Options.php:它是一个模型文件,为配置的某些表单元素提供选项。

app/code/local/Envato/CustomConfig/Helper/Data.php:这是Magento助手系统使用的文件。

我们重点看system.xml和config.xml是怎么配置的。

config.xml
<config>
       <modules>
        <Envato_CustomConfig>
            <version>0.0.1</version>
        </Envato_CustomConfig>
    </modules>
    <global>
        <helpers>
            <customconfig>
                <class>Envato_CustomConfig_Helper</class>
            </customconfig>
        </helpers>
        <models>
             <customconfig>
                <class>Envato_CustomConfig_Model</class>
             </customconfig>
        </models>
    </global>
   <adminhtml>
        <acl>
            <resources>
                <admin>
                    <children>
                        <system>
                            <children>
                                <config>
                                    <children>
                                        <customconfig_options>
                                            <title>Custom Configuration Section</title>
                                        </customconfig_options>
                                    </children>
                                </config>
                            </children>
                        </system>
                    </children>
                </admin>
            </resources>
        </acl>
    </adminhtml>
   ....
</config>

<adminhtml>标签被用来定义管理员方面的资源。在我们的示例中,我们模块的配置页面是一个资源。此外,我们使用 <acl>标记来包装<resources>标记,这意味着它只能由经过身份验证的用户访问。
接下来的几个标签用于定义后端配置页面的路径。因此,配置页面的路径看起来像“admin/system/config/customconfig_options”。当然,最后一个标签<customconfig_options>应该是唯一的,因此它不会与其他扩展冲突。

system.xml
<?xml version="1.0"?>
<config>
    <tabs>
        <customconfig translate="label" module="customconfig">
            <label>Custom Configuration Tab</label>
            <sort_order>1</sort_order>
        </customconfig>
    </tabs>
    <sections>
        <customconfig_options translate="label" module="customconfig">
            <label>Custom Configuration Settings</label>
            <tab>customconfig</tab>
            <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>
            <groups>
                <section_one translate="label">
                    <label>Section One</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>
                        <custom_field_one>
                            <label>Custom Text Field</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>
                            <comment>Example of text field.</comment>                   
                        </custom_field_one>
                    </fields>
                </section_one>
                <section_two translate="label">
                    <label>Section Two</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>1</show_in_store>                
                    <fields>
                        <custom_field_two>
                            <label>Custom Select Field</label>
                            <frontend_type>select</frontend_type>
                            <source_model>customconfig/options</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>
                            <comment>Example of select field.</comment>
                        </custom_field_two>
                        <custom_field_three>
                            <label>Custom Radio Field</label>
                            <frontend_type>radios</frontend_type>
                            <source_model>customconfig/options</source_model>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Example of radios field.</comment>
                        </custom_field_three>
                        <custom_field_four>
                            <label>Custom Multiselect Field</label>
                            <frontend_type>multiselect</frontend_type>
                            <source_model>customconfig/options</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>
                            <comment>Example of multiselect field.</comment>
                        </custom_field_four>
                    </fields>
                </section_two>
            </groups>                
        </customconfig_options>
    </sections>
</config>

<tabs>标签被用于定义将在配置页的左手侧显示的选项卡。而<sections>标签使用的每个选项卡下定义的部分。

我们使用<groups>标记将类似的配置字段组合在一起。例如,您希望在不同部分下显示与图像相关的字段和电子邮件相关字段。为此,我们定义<section_one><section_two>标签。

最后,我们用<fields>标记包装每个部分的字段 。在我们的示例中,我们在 “Section One”字段集下提供了一个文本字段,而在“Section Two”字段集下可以使用其他字段,如选择框,单选按钮和多选。这就是“system.xml”文件。

接下来,让我们创建一个模型文件"app / code / local / Envato / CustomConfig / Model / Options.php"

<?php
class Envato_CustomConfig_Model_Options
{
  /**
   * Provide available options as a value/label array
   *
   * @return array
   */
  public function toOptionArray()
  {
    return array(
      array('value'=>1, 'label'=>'One'),
      array('value'=>2, 'label'=>'Two'),
      array('value'=>3, 'label'=>'Three'),            
      array('value'=>4, 'label'=>'Four')                     
    );
  }
}

最后,我们需要创建“app / code / local / Envato / CustomConfig / Helper / Data.php”文件,以确保Magento的翻译系统正常工作。它几乎是一个空文件,但应按照惯例存在!

测试自定义配置

前往Magento的后端并清除所有缓存。现在,转到System > Configuration以打开配置页面。您应该注意 页面左侧的CUSTOM CONFIGURATION TAB。在此之下,可以使用“自定义配置设置”链接,单击该链接将打开我们模块的配置页 面。如果您看不到它,请尝试退出管理部分并再次登录。
在这里插入图片描述
您需要填写表单并单击Save Config 按钮提交更改,Magento将处理其余部分。

要访问代码中配置参数的值,可以使用getStoreConfig静态方法。例如,您可以custom_field_one使用以下语法检索参数的值 :

Mage::getStoreConfig('customconfig_options/section_one/custom_field_one');

语法非常简单 - 您需要使用system.XML文件中定义的“section_name / group_name / field_name”模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迁就i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值