2019-06-12

Magento系统配置的使用与自定义配置

系统配置的使用和存储方法

Magento的系统配置界面为System=>Configuration

在数据库中存储的数据表为core_config_data

在程序中获取系统配置的API为Mage::getStoreConfig(core_config_data中的path)

 

新增自定义系统配置

在模块的etc目录下新增system.xml,如:

[code lang="xml"]<?xml version="1.0" ?><config>
<tabs>
<etam_blog translate="label" module="etam_blog">
<label>Etam Blog</label>
<sort_order>1</sort_order>
</etam_blog>
</tabs>
<sections>
<etam_blog_options translate="label" module="etam_blog">
<label>Configurations</label>
<tab>etam_blog</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>
<confs>
<label>General</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>
<blog_title>
<label>Blog Title</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>
</blog_title>
<show_posts>
<label>Number of posts shown per page</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>
</show_posts>
<list_type>
<label>List type</label>
<frontend_type>select</frontend_type>
<source_model>etam_blog/ListTypes</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>
</list_type>
</fields>
</confs>
<repls>
<label>Reply Settings</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>
<reply_email>
<label>Send Email to composer when get reply</label>
<frontend_type>checkbox</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>
</reply_email>
</fields>
</repls>
</groups>
</etam_blog_options>
</sections>
</config>[/code]

System.xml配置讲解

<tabs>为配置模块,展现为Configuration页面左侧块,如GENERAL, CATALOG等,此处是ETAM BLOG, Magento在显示时会将英文label转化为大写

<sections>为配置模块下的菜单项,一个section对应一个页面,如没有配置ACL,则magento会将页面跳转至404 Not Found

<groups>对应一个section页面中可折叠的配置组,其中包括一个或多个配置项

<fields>配置组中的配置项定义

<frontend_type>只有在fileds中才有意义,定义了该配置项对应的HTML控件类型

<source_model>配置项列表中对应的数据源Model,配置项列表的显示是调用该类的toOptionArray方法

增加ACL信息至config.xml

[code lang="xml"]<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<etam_blog_options>
<title>Etam Blog Configurations</title>
</etam_blog_options>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
[/code]

config.xml中acl配置讲解

<adminhtml>后台配置块

<acl>ACL配置块

<children>URI路径分隔,如admin/system_config,则分为<admin><children><system><children><config>

<etam_blog_options>对应system.xml中的section名称

<title>配置页面中的标题

增加配置项候选列表Model

[code lang="php"]class Etam_Blog_Model_ListTypes extends Mage_Core_Model_Abstract
public function toOptionArray()
return array(
array('value'=>'TITLES_ONLY', 'label'=>Mage::helper('etam_blog')->__('Titles only')),
array('value'=>'FULL_CONTENTS', 'label'=>Mage::helper('etam_blog')->__('Full contents')),
array('value'=>'SUMMARY_ONLY', 'label'=>Mage::helper('etam_blog')->__('Sumary only'))
);

[/code]

配置项列表所使用的Model只需实现toOptionArray方法即可

没有评论:

发表评论