扩展自定义模块的Varien表单元素 [英] Extend a Varien Form Element for a Custom Module

查看:30
本文介绍了扩展自定义模块的Varien表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此问题上的改进: 是吗在lib/Varien/Data/Form/Element文件夹中添加自己的文件的好方法

Improving on this question: Is it good practice to add own file in lib/Varien/Data/Form/Element folder

可接受的答案显示了如何扩展Varien表单元素,但是如果要将其打包到自定义模块中将无法正常工作.

The accepted answer shows how to extend a Varien form element, but this will not work if you want to package it into a custom module.

在模块中扩展Varien表单元素的正确方法是什么?我希望有一个简单的XML设置?

What would be the proper method of extending the Varien form element in a module? A simple XML setting I'm hoping?

更新:

感谢Vinai的回复.尽管这样做确实可行,但我还是希望以某种方式扩展form元素.我的扩展程序使用基本的File表单元素来允许管理员将文件上传到类别.因此,我自己并没有直接将表单元素添加到字段集中.

Thanks Vinai for the response. Although that does work, I was hoping to extend the form element somehow. My extension is using the base File form element to allow administrators to upload files to categories. So, I'm not directly adding the form elements to the fieldset myself.

我想可以检查后端我的类别块上输入的文件:Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes,然后将表单元素(如果是'file')更改为'mycompany_file',但这似乎是一种解决方法

I suppose it's possible to to check for the file input on my category block on the backend: Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes , and then change the form element if it is 'file' to 'mycompany_file' -- but this seems like a workaround.

有没有更简单的方法?再次感谢Vinai.

Is there an easier way? Thanks again Vinai.

推荐答案

Varien_Data_Form实例上,您可以指定自定义元素类型,如下所示:

On the Varien_Data_Form instance you can specify custom element types like this:

$fieldset->addType('custom', 'Your_Module_Model_Form_Element_Custom');

然后,用
添加元素

Then, add your element with

$fieldset->addField('the_name', 'custom', $optionsArray);

如果您使用的是没有字段集的表单,那么您也可以在Varien_Data_Form实例上执行相同的操作.

If you are using a form without fieldsets you can do the same on the Varien_Data_Forminstance, too.

编辑:由于问题中新增了其他详细信息,因此扩展了答案.

Expand answer because of new additional details in the question.

在类Mage_Adminhtml_Block_Widget_Form::_setFieldset()中,有以下代码:

In the class Mage_Adminhtml_Block_Widget_Form::_setFieldset() there is the following code:

$rendererClass  = $attribute->getFrontend()->getInputRendererClass();
if (!empty($rendererClass)) {
    $fieldType  = $inputType . '_' . $attribute->getAttributeCode();
    $fieldset->addType($fieldType, $rendererClass);
}

因此,属性上的属性frontend_input_renderer可用于指定自定义元素类.
可以在表catalog_eav_attribute中找到此属性,幸运的是,没有为任何类别图像属性设置此属性.

Because of this the attribute frontend_input_renderer on the attributes can be used to specify custom element classes.
This property can be found in the table catalog_eav_attribute, and luckily enough it isn't set for any of the category image attributes.

鉴于此,有几种应用定制的方法.

Given this, there are several ways to apply customizaton.

一种选择是简单地使用升级脚本在表中设置元素类.
另一个可能是使用eav_entity_attribute_load_after事件的观察者,并在entity_type_id和输入类型匹配时即时设置输入渲染器.

One option is to simply set the element class in the table using an upgrade script.
Another would be using an observer for the eav_entity_attribute_load_after event and setting the input renderer on the fly if the entity_type_id and the input type matches.

因此,与Magento中的常规类重写相比,它涉及的程度要多一些,但这是很有可能的.

So it is a little more involved then just regular class rewrites in Magento, but it is quite possible.

这篇关于扩展自定义模块的Varien表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆