14

有没有办法将自定义表单元素添加到 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实例化/渲染 a 。Varien_Data_Form_Element_Supertextfield

这很好,但这意味着我需要在lib/Varian文件夹层次结构中包含我的自定义表单元素。鉴于 Magento 是如何面向模块的,这似乎是错误的。

我意识到我可以在 lib 中使用 custo 自动加载器或符号链接,但我主要有兴趣了解是否有

  1. 为属性添加自定义表单元素的规范方法

  2. 自定义 Magento 自动加载器的规范方法。

4

3 回答 3

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;
    }
}
于 2010-05-18T21:51:19.010 回答
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 类的情况下工作。

于 2010-02-04T12:12:34.400 回答
2

自助服务台再次罢工。看起来 Magento 设置包含路径的方式是,您可以从本地代码分支中的 lib(而不仅仅是从 Mage_ 命名空间)中删除类文件

app/code/local/Varien/etc

当自动加载器尝试加载 lib/Varien 类时,它会首先检查您的目录。如果 Varien 创建与您的名称相同的数据元素,这仍然会使您面临风险,但随着风险的增加,风险相对较低。

于 2010-01-03T19:46:40.310 回答