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

标签 magento

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

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

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

Block/Adminhtml/Edit/Tabs.php block 在左侧创建了 2 个选项卡:General InfoItem Images,每个他们中的一些人使用 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();
}

我希望选项卡 item_images 呈现我自己的表单元素和值,而不是默认的变体表单元素。

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

最佳答案

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

lib/Varien/Data/Form/Element/Text.php

你应该在本地或社区代码池中放置一个文件

app/code/local/Varient/Data/Form/Element/Text.php

但是,执行替换类,维护与 future 版本的兼容性成为您的责任。这意味着如果 Magento Inc. 更改 lib/Varien/Data/Form/Element/Text.php,您需要更新您的版本才能兼容。

根据您所说的,我会考虑为呈现表单的 Block 类创建类重写。

关于magento - 在 lib/Varien/Data/Form/Element 文件夹中添加自己的文件是一种好习惯吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6073891/

相关文章:

magento - 在 Google Analytics 中实现增强型电子商务

Magento 扩展 cms block 类

玛根托 : Fatal error : Call to a member function getFrontend() on a non-object

html - 在某个类的正文中选择某个类的链接

php - fatal error : Call to a member function count() on a non-object in

php - 一页 magento 网站出现问题

php - 错误处理请求 : Error in file: - SQLSTATE[42S01]: Base table or view already exists:

magento - 主题内的渲染 block 和子 block

database - 从 Magento 数据库获取查询——mysql_num_rows

mysql - Magento 获取设定日期之间的订单总和