在lib/Varien/Data/Form/Element文件夹中添加自己的文件是一种好习惯

zok*_*mkd 3 magento

我需要在Magento中创建具有少量数据库表的模块.该模块的一个功能是添加多个图像.例如,在管理员的" 添加新项目 "或" 编辑项目 "页面上,从左侧我有标签,其中一个是" 项目图像 ".单击时,我希望此选项卡的内容是我自己的自定义内容.在深入研究代码后,发现它呈现这种内容的方式,Magento正在为完整形式的每个元素使用一个Varien_Data_Form_Element类.我想在这里添加我自己的类,它将以我想要的方式呈现表单元素.这是一个很好的做法,还是有一些其他更优雅的方式在管理表单中添加自己的内容?编辑:我必须补充说,现有的课程都没有帮助我解决问题.

解决方案编辑:我的自定义模块中有一个控制器,位于Mypackage/Mymodule/controllers/Adminhtml/Item.php中.在我用于添加和创建新项目的editAction()方法中,我创建了2个块,一个用于表单,另一个用于选项卡:

$this->_addContent($this->getLayout()->createBlock('item/adminhtml_edit'))
                    ->_addLeft($this->getLayout()->createBlock('item/adminhtml_edit_tabs'));
$this->renderLayout();

座/ Adminhtml /编辑/ Tabs.php块是创建左边2个选项卡:一般信息产品图片,他们每个人都渲染上使用Block类右侧不同的内容.

protected function _beforeToHtml()
{
   $this->addTab('item_info', array(
        'label' => Mage::helper('mymodule')->__('Item Info'),
        'content'=> $this->getLayout()->createBlock('item/adminhtml_edit_tab_form')->toHtml(),
        ));

   $this->addTab('item_images', array(
        'label' => Mage::helper('mymodule')->__('Item Images'),
        'active' => ( $this->getRequest()->getParam('tab') == 'item_images' ) ? true : false,
        'content' => $this->getLayout()->createBlock('item/adminhtml_images')->toHtml(),
        ));

   return parent::_beforeToHtml();
}

我希望tab_images选项卡能够呈现我自己的表单元素和值,而不是默认的varien表单元素.

class Mypackage_Mymodule_Block_Adminhtml_Images extends Mage_Core_Block_Template
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('item/images.phtml'); //This is in adminhtml design
    }


    public function getPostId()
    {
    return $this->getRequest()->getParam('id');
    }

    public function getExistingImages()
    {
        return Mage::getModel('mymodule/item')->getImages($this->getPostId());
    }
}

然后在模板app/design/adminhtml/default/default/template/item/images.phtml中,您可以使用以下值:

//You can add your own custom form fields here and all of them will be included in the form
foreach($this->getExistingImages() as $_img):
//Do something with each image
endforeach;
//You can add your own custom form fields here and all of them will be included in the form

Ala*_*orm 5

不,这不对.您永远不应编辑或添加供应商提供的文件.如果绝对必须替换类文件,则应使用本地代码池.例如,如果要更改文本字段的行为,

lib/Varien/Data/Form/Element/Text.php
Run Code Online (Sandbox Code Playgroud)

您应该将文件放在本地或社区代码池中

app/code/local/Varient/Data/Form/Element/Text.php
Run Code Online (Sandbox Code Playgroud)

但是,在替换类时,您有责任保持与未来版本的兼容性.这意味着如果Magento Inc.发生变化lib/Varien/Data/Form/Element/Text.php,您需要更新您的版本才能兼容.

根据你所说的,我将研究为渲染表单的Block类创建一个类重写.