有没有办法将自定义表单元素添加到Magento Adminhtml表单而不将自定义元素放在lib/Varian
文件夹中?
我已经跟踪了基本上是Varian_Data_Form_Element_
工厂
public function addField($elementId, $type, $config, $after=false)
{
if (isset($this->_types[$type])) {
$className = $this->_types[$type];
}
else {
$className = 'Varien_Data_Form_Element_'.ucfirst(strtolower($type));
}
$element = new $className($config);
$element->setId($elementId);
if ($element->getRequired()) {
$element->addClass('required-entry');
}
$this->addElement($element, $after);
return $element;
}
所以,如果我正确读取这个,我确保EAV属性的前端返回特定的fieldType(例如supertextfield
),系统将在显示时实例化/呈现Varien_Data_Form_Element_Supertextfield
属性的编辑形式。
这很好,但这意味着我需要在lib/Varian
文件夹层次结构中包含我的自定义表单元素。考虑到面向模块的Magento,看起来这样做是错误的。
我意识到我可以在lib中使用custo自动加载器或符号链接,但我主要是想学习是否有
为属性
自定义Magento自动加载器的规范方法。
答案 0 :(得分:26)
这是一篇旧帖子,但对某人来说仍然有用:
是的,你可以。
以下代码位于: app / code / local / MyCompany / MyModule / Block / MyForm.php
class MyCompany_MyModule_Block_MyForm extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/save'),
'method' => 'post'
));
$fieldset = $form->addFieldset('my_fieldset', array('legend' => 'Your fieldset title')));
//Here is what is interesting us
//We add a new type, our type, to the fieldset
//We call it extended_label
$fieldset->addType('extended_label','MyCompany_MyModule_Lib_Varien_Data_Form_Element_ExtendedLabel');
$fieldset->addField('mycustom_element', 'extended_label', array(
'label' => 'My Custom Element Label',
'name' => 'mycustom_element',
'required' => false,
'value' => $this->getLastEventLabel($lastEvent),
'bold' => true,
'label_style' => 'font-weight: bold;color:red;',
));
}
}
以下是自定义元素的代码,位于app / code / local / MyCompany / MyModule / Lib / Varien / Data / Form / Element / ExtendedLabel.php :
class MyCompany_MyModule_Lib_Varien_Data_Form_Element_ExtendedLabel extends Varien_Data_Form_Element_Abstract
{
public function __construct($attributes=array())
{
parent::__construct($attributes);
$this->setType('label');
}
public function getElementHtml()
{
$html = $this->getBold() ? '<strong>' : '';
$html.= $this->getEscapedValue();
$html.= $this->getBold() ? '</strong>' : '';
$html.= $this->getAfterElementHtml();
return $html;
}
public function getLabelHtml($idSuffix = ''){
if (!is_null($this->getLabel())) {
$html = '<label for="'.$this->getHtmlId() . $idSuffix . '" style="'.$this->getLabelStyle().'">'.$this->getLabel()
. ( $this->getRequired() ? ' <span class="required">*</span>' : '' ).'</label>'."\n";
}
else {
$html = '';
}
return $html;
}
}
答案 1 :(得分:3)
类Varien_Data_Form_Abstract
有一个方法addType()
,您可以在其中添加新元素类型及其各自的类名。要利用此功能,您可以将块Mage_Adminhtml_Block_Widget_Form
复制到本地代码池并扩展方法_getAdditionalElementTypes()
:
protected function _getAdditionalElementTypes()
{
$types = array(
'my_type' => 'Namespace_MyModule_Block_Widget_Form_Element_MyType',
);
return $types;
}
由于类Mage_Adminhtml_Block_Widget_Form
是所有其他表单类的基类,不幸的是只是重写配置中的块将不起作用。
编辑:如果您只需要一个表单中的自定义元素类型,则可以覆盖特定类,并通过覆盖方法_getAdditionelElementTypes()
来添加类型。这比将importend magento类复制到本地代码池更简洁。
EDIT2:查看Mage_Adminhtml_Block_Widget_Form::_setFieldset()
还有另一种可能性:如果属性的值为frontend_input_renderer
(例如mymodule / element_mytype),则会加载具有该名称的块。另请参见Mage / Eav / Model / Entity / Attribute / Frontend / Abstract.php第160行。这应该不会覆盖任何Magento类。
答案 2 :(得分:2)
自助服务台再次罢工。看起来Magento以这样的方式设置包含路径,您可以从本地代码分支中的lib(不仅仅是Mage_命名空间)中删除类文件
app/code/local/Varien/etc
当自动加载器尝试加载lib / Varien类时,它将首先检查您的目录。如果Varien创建了一个与您的名称相同的数据元素,这仍然会使您处于危险之中,但风险相对较低。