如何在 magento 1 中创建自定义系统字段?
How to create a custom system field in magento 1?
Magento 1.9
我想在“系统”>“配置”中创建一个新选项卡。
在这个选项卡中,我需要一个组选项卡,在这个组选项卡中,我想要一个连接到我的数据库字段的文本区域。如果我编辑我的文本区域,它也会修改我的数据库字段。
我不知道如何将我的 textarea 与我的数据库连接。使用新组创建一个新选项卡很简单,但将其连接到数据库。
谢谢!
假设您想要一个包含 2 个字段的部分:multiselect、文本区域。在 multiselect 中你有所有的客户,在文本中你得到修改以申请某个数据库字段(与客户相关)。
你的 system.xml 应该是这样的:
<config>
<tabs>
<admin_customTab_2 translate="label" module="sections">
<label>- TAB NAME -</label>
<sort_order>2</sort_order>
</admin_customTab_2>
</tabs>
<sections>
<customsection2 translate="label" module="sections">
<label>label name</label>
<tab>admin_customTab_2</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>
<block translate="label">
<label>label name</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>
<block_customers>
<label>Select user</label>
<comment>user list</comment>
<frontend_type>multiselect</frontend_type>
<backend_model>sections/users</backend_model>
<source_model>sections/users</source_model> <!-- adding a source-model for the form's select -->
<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>
</block_customers>
<block_textarea>
<label>changes to apply</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>
</block_textarea>
</fields>
</block>
</groups>
</customsection2>
</sections>
这对于配置来说还不够,您需要将访问控制列表 (ACL) 告诉 Magento,否则管理员用户将看不到它。它是在 config.xml
像这样:
<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<customsection2>
<title>Customer Changes?</title>
</customsection2>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
一切都已设置,但除了向我们显示选项卡和表单之外什么也没做。
如果您足够勇敢,您会注意到此处 system.xml 代码中的一个标记,
<backend_model>sections/users</backend_model>
<source_model>sections/users</source_model> <!-- adding a source-model for the form's select -->
这告诉magento你正在使用与这个表单相关的模型,你可以在其中进行操作。
因此,在您的模型路径中创建(在此示例中)User.php
我们开始吧:
您想用这个来扩展您的自定义 class。
class Admin_Sections_Model_Users 扩展 Mage_Core_Model_Config_Data
是的,但我们仍然没有在表单中插入任何数据。
你可以通过添加一个特殊的函数来做到这一点 toOptionArray() :
public function toOptionArray()
{
$collections = Mage::getModel("customer/customer")->getCollection();
foreach ($collections as $colletion){
$user = Mage::getModel("customer/customer")->load($colletion->getId());
$array[] = array('value'=>$user->getEntityId(),'label'=>Mage::helper("sections")->__($user->getName()));
}
return $array ;
}
这会将所有客户的姓名设置为多 select 形式。
"yeah, nice.. I asked about the text-area"
在告诉你如何从 Store Configs 中获取数据之前,你应该知道有两种主要的方法来详细说明表单数据:
public function _afterSave()
{}
public function _beforeSave()
{}
你可以把你所有的代码放在那里,不需要解释它们之间有什么区别。
在此函数中,您可以通过以下操作轻松地从存储配置中获取数据:
$text_area_string = Mage::getStoreConfig('customsection2/block/block_textarea');
"yeah nice... but my main problem is to save things in DB"
您可以随时使用 Mage::getModel('') 方法,这可以将您连接到数据库。让我们举个例子;修改 'customer_entity' table.
上的 'is_active' 字段
$customer = Mage::getModel("customer/customer")->load($customer_id);
$customer->setData("is_active",0);
$customer->save();
这个字段实际上没有做任何事情,它只是一个非常快速的例子。
这应该让你做你的目标。
如果有什么奇怪的地方,请告诉我!
Magento 1.9
我想在“系统”>“配置”中创建一个新选项卡。
在这个选项卡中,我需要一个组选项卡,在这个组选项卡中,我想要一个连接到我的数据库字段的文本区域。如果我编辑我的文本区域,它也会修改我的数据库字段。
我不知道如何将我的 textarea 与我的数据库连接。使用新组创建一个新选项卡很简单,但将其连接到数据库。
谢谢!
假设您想要一个包含 2 个字段的部分:multiselect、文本区域。在 multiselect 中你有所有的客户,在文本中你得到修改以申请某个数据库字段(与客户相关)。
你的 system.xml 应该是这样的:
<config>
<tabs>
<admin_customTab_2 translate="label" module="sections">
<label>- TAB NAME -</label>
<sort_order>2</sort_order>
</admin_customTab_2>
</tabs>
<sections>
<customsection2 translate="label" module="sections">
<label>label name</label>
<tab>admin_customTab_2</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>
<block translate="label">
<label>label name</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>
<block_customers>
<label>Select user</label>
<comment>user list</comment>
<frontend_type>multiselect</frontend_type>
<backend_model>sections/users</backend_model>
<source_model>sections/users</source_model> <!-- adding a source-model for the form's select -->
<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>
</block_customers>
<block_textarea>
<label>changes to apply</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>
</block_textarea>
</fields>
</block>
</groups>
</customsection2>
</sections>
这对于配置来说还不够,您需要将访问控制列表 (ACL) 告诉 Magento,否则管理员用户将看不到它。它是在 config.xml 像这样:
<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<customsection2>
<title>Customer Changes?</title>
</customsection2>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
一切都已设置,但除了向我们显示选项卡和表单之外什么也没做。 如果您足够勇敢,您会注意到此处 system.xml 代码中的一个标记,
<backend_model>sections/users</backend_model>
<source_model>sections/users</source_model> <!-- adding a source-model for the form's select -->
这告诉magento你正在使用与这个表单相关的模型,你可以在其中进行操作。 因此,在您的模型路径中创建(在此示例中)User.php 我们开始吧:
您想用这个来扩展您的自定义 class。 class Admin_Sections_Model_Users 扩展 Mage_Core_Model_Config_Data
是的,但我们仍然没有在表单中插入任何数据。 你可以通过添加一个特殊的函数来做到这一点 toOptionArray() :
public function toOptionArray()
{
$collections = Mage::getModel("customer/customer")->getCollection();
foreach ($collections as $colletion){
$user = Mage::getModel("customer/customer")->load($colletion->getId());
$array[] = array('value'=>$user->getEntityId(),'label'=>Mage::helper("sections")->__($user->getName()));
}
return $array ;
}
这会将所有客户的姓名设置为多 select 形式。
"yeah, nice.. I asked about the text-area" 在告诉你如何从 Store Configs 中获取数据之前,你应该知道有两种主要的方法来详细说明表单数据:
public function _afterSave()
{}
public function _beforeSave()
{}
你可以把你所有的代码放在那里,不需要解释它们之间有什么区别。
在此函数中,您可以通过以下操作轻松地从存储配置中获取数据:
$text_area_string = Mage::getStoreConfig('customsection2/block/block_textarea');
"yeah nice... but my main problem is to save things in DB"
您可以随时使用 Mage::getModel('') 方法,这可以将您连接到数据库。让我们举个例子;修改 'customer_entity' table.
上的 'is_active' 字段 $customer = Mage::getModel("customer/customer")->load($customer_id);
$customer->setData("is_active",0);
$customer->save();
这个字段实际上没有做任何事情,它只是一个非常快速的例子。 这应该让你做你的目标。 如果有什么奇怪的地方,请告诉我!