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

发布于 2024-11-08 20:52:49 字数 2500 浏览 0 评论 0原文

我需要在 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();

Block/Adminhtml/Edit /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();
}

我希望选项卡 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

I need to create module in Magento which will have few database tables. One of the function of the module is adding multiple images.
For example while being on the "Add new item" or "Edit item" page in the admin, from the left side I have tabs, one of them is "Item Images". When being clicked I want the content of this tab to be my own custom one.
After digging into the code, found out that the way it renders this content, Magento is using one of the Varien_Data_Form_Element classes for each element in the full form. I want to add my own class here that will render form elements the way I want.
Is this a good practice to do so, or there is some other more elegant way of adding own content in the admin forms?
EDIT: I must add that none of the existing classes is helping my problem.

SOLUTION EDIT:
I have a controller in my custom module that is in Mypackage/Mymodule/controllers/Adminhtml/Item.php. In the editAction() method which I am using for adding and creating new items, I am creating 2 blocks, one for the form and one left for the tabs:

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

The Block/Adminhtml/Edit/Tabs.php block is creating 2 tabs on the left: General Info and Item Images, each of them are rendering different content on the right side using Block classes.

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

I wanted the tab item_images to render my own form elements and values, not the default varien form elements.

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

Then in the template app/design/adminhtml/default/default/template/item/images.phtml you can use these values:

//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

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

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

发布评论

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

评论(1

梦幻的心爱 2024-11-15 20:52:49

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

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

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

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

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

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

No, it's not. You should never edit or add to files that provided by a vendor. If you absolutely must replace a class file you should use the local code pool. For example, if you wanted to change the behavior of a text field,

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

You should place a file in the local or community code pool

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

However, doing the replaces the class, and it becomes your responsibility to maintain compatibility with future versions. That means if Magento Inc. changes lib/Varien/Data/Form/Element/Text.php, you need to update your version to be compatible.

Based on what you said I'd look into creating a class rewrite for the Block class that renders the form.

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