将自定义表单元素添加到 Adminhtml 表单

发布于 2024-08-16 13:47:17 字数 1084 浏览 2 评论 0 原文

有没有办法将自定义表单元素添加到 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 是如何面向模块的,这似乎是错误的。

我意识到我可能会在库中使用 custo 自动加载器或符号链接,但我主要感兴趣的是了解是否存在

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

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

Is there a way to add a custom form element to a Magento Adminhtml form without placing the custom element in the lib/Varian folder?

I've tracked down the code that's essentially a Varian_Data_Form_Element_ factory

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;
}

So, if I'm reading this correctly, I ensure that an EAV attribute's frontend returns a specific fieldType, (such as supertextfield) and the system will instantiate/render a Varien_Data_Form_Element_Supertextfield when displaying this attribute's editing form.

This is well and good, but it means I need to include my custom form element in the lib/Varian folder hierarchy. Given how module oriented Magento is, it seems like this is doing it wrong.

I realize I could jank around with a custo autoloader or symlinks in the lib, but I'm primarily interested in learning if there's

  1. A canonical way to add custom form elements for attributes

  2. A canonical way to customize the Magento autoloader.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑脸一如从前 2024-08-23 13:47:17

这是一篇旧文章,但它对某人仍然有用:

是的,你可以。

下面的代码位于:
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 中/扩展标签.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;
    }
}

This is an old post but it still can be usefull for someone :

yes you can.

The code below is located in :
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;',
        ));
    }
}

Here is the code of your custom element, which is located in 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;
    }
}
七堇年 2024-08-23 13:47:17

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 类的情况下工作。

The class Varien_Data_Form_Abstract has a method addType() where you can add new element types and their respective class names. To exploit this functionality you can copy the block Mage_Adminhtml_Block_Widget_Form to the local code pool and extend the method _getAdditionalElementTypes():

protected function _getAdditionalElementTypes()
{
    $types = array(
        'my_type' => 'Namespace_MyModule_Block_Widget_Form_Element_MyType',
    );

    return $types;
}

As the class Mage_Adminhtml_Block_Widget_Form is a base class for all the other form classes, unfortunately just rewriting the block in the config will not work.

EDIT: If you need the custom element types in just one form you could override the specific class and add the type there by overriding the method _getAdditionelElementTypes(). This would be a cleaner solution than copying an importend magento class to the local code pool.

EDIT2: Looking at Mage_Adminhtml_Block_Widget_Form::_setFieldset() there is another possibility: If the attribute has a value in frontend_input_renderer (e.g. mymodule/element_mytype) then a block with that name is loaded. See also Mage/Eav/Model/Entity/Attribute/Frontend/Abstract.php line 160. This should work without overriding any Magento classes.

北凤男飞 2024-08-23 13:47:17

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

app/code/local/Varien/etc

当自动加载器尝试加载 lib/Varien 类时,它将检查您的目录第一的。如果 Varien 创建与您同名的数据元素,您仍然面临风险,但风险相对较低。

Self help desk strikes again. It looks like Magento sets up include paths in such a way that you can drop class files from lib (not just from the Mage_ namespace) in your local code branch

app/code/local/Varien/etc

When the autoloader tries to load a lib/Varien class, it will check your directory first. This still puts you at risk if Varien ever creates a data element with the same name as yours, but as risks go it's relatively low.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文