如何将 magento 小部件表单设置为 enctype="multipart/form-data"用于文件上传?

发布于 2024-10-27 19:08:32 字数 3280 浏览 1 评论 0 原文

我正在尝试为画廊制作一个小部件,因为我见过的所有模块都没有真正实现我所追求的功能,而小部件似乎是允许最终用户对放置进行良好且轻松控制的前进方向。

到目前为止,我已经遵循了 Magento 如何制作小部件教程:

http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-2/

这允许我设置文件我需要并让事情在后端得到认可。

现在我可以通过下面的代码添加图像上传字段,但文件未上传,这似乎是由于当我添加文件字段时未自动设置 enctype="multipart/form-data" 。

所以我查看了一下,似乎您可以为

local/WebsiteDevelopment/GalleryWidget/etc/Widget.xml

<?xml version="1.0"?>
<widgets>
    <WebsiteDevelopment_GalleryWidget type="widgets/list" translate="name description" module="GalleryWidget">
        <name>Banner Gallery</name>
        <description>Adds a full page width gallery</description>
        <parameters>
            <enabled_services>
                <label>Enabled Services</label>
                <visible>1</visible>
                <required>1</required>
                <type>multiselect</type>
                <source_model>WebsiteDevelopment_GalleryWidget_Model_Services</source_model>
            </enabled_services>
            <helper_block>
                <type>WebsiteDevelopment/GalleryWidget_Adminhtml_Edit_Form</type>
            </helper_block>
            <template translate="label">
                <label>Frontend Template</label>
                <visible>1</visible>
                <required>1</required>
                <type>select</type>
                <values>
                    <text translate="label">
                        <value>GalleryWidget/view.phtml</value>
                        <label>Text Links</label>
                    </text>
                </values>
            </template>
            <image>
                <label>Image One</label>
                <description>The first image for the banner</description>
                <visible>1</visible>
                <type>image</type>
            </image>
        </parameters>
    </WebsiteDevelopment_GalleryWidget>
</widgets> 

形式的小部件添加一个帮助程序块,因此在我将我的帮助程序块添加到上面 xml 的 params 部分后,我在 WebsiteDevelopment/ 创建我的帮助程序GalleryWidget/Block/Adminhtml/Edit/Form.php (我认为它应该在哪里,我仍然不确定块是否必须采用这样的特定结构才能在后端使用),然后添加以下代码

WebsiteDevelopment /GalleryWidget/Block/Adminhtml/Edit/Form.php

<?php
class WebsiteDevelopment_GalleryWidget_Block_Adminhtml_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'enctype' => 'multipart/form-data'
        ));

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

显然,该表单需要更多属性才能充分工作,但我只是希望在表单上获得一些不同的属性,以便我可以确保该块已被应用。

因此,当我进入后端时,带有帮助程序的表单仍然没有 enctype,并且我不确定我的帮助程序块是否正在加载,或者是否正在加载,但它只是没有任何效果。

我是否以正确的方式处理这件事?

I am trying to make a widget for a gallery as all the modules I have seen don't really do what I am after and widgets seem like the way forward to allow the end user to have nice and easy control over placement.

So so far I have followed the Magento how to make a widget tutorial:

http://www.magentocommerce.com/knowledge-base/entry/tutorial-creating-a-magento-widget-part-2/

which has allowed me to set up the files I need and have things recognised in the back end.

Now I can add an image upload field through the code below but the file does not get uploaded, this is it seems due to enctype="multipart/form-data" not being set automatically when I add a file field.

So I looked about and it seems you can add a helper block for the widget form

local/WebsiteDevelopment/GalleryWidget/etc/Widget.xml

<?xml version="1.0"?>
<widgets>
    <WebsiteDevelopment_GalleryWidget type="widgets/list" translate="name description" module="GalleryWidget">
        <name>Banner Gallery</name>
        <description>Adds a full page width gallery</description>
        <parameters>
            <enabled_services>
                <label>Enabled Services</label>
                <visible>1</visible>
                <required>1</required>
                <type>multiselect</type>
                <source_model>WebsiteDevelopment_GalleryWidget_Model_Services</source_model>
            </enabled_services>
            <helper_block>
                <type>WebsiteDevelopment/GalleryWidget_Adminhtml_Edit_Form</type>
            </helper_block>
            <template translate="label">
                <label>Frontend Template</label>
                <visible>1</visible>
                <required>1</required>
                <type>select</type>
                <values>
                    <text translate="label">
                        <value>GalleryWidget/view.phtml</value>
                        <label>Text Links</label>
                    </text>
                </values>
            </template>
            <image>
                <label>Image One</label>
                <description>The first image for the banner</description>
                <visible>1</visible>
                <type>image</type>
            </image>
        </parameters>
    </WebsiteDevelopment_GalleryWidget>
</widgets> 

so after i add my helper block into the params section of the xml above i create my helper at WebsiteDevelopment/GalleryWidget/Block/Adminhtml/Edit/Form.php (which i think is where it should be, i am still unsure whether a block has to be in a specific structure like this for use in the backend ) and then add the following code

WebsiteDevelopment/GalleryWidget/Block/Adminhtml/Edit/Form.php

<?php
class WebsiteDevelopment_GalleryWidget_Block_Adminhtml_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
        $form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'enctype' => 'multipart/form-data'
        ));

        $form->setUseContainer(true);
        $this->setForm($form);
        return parent::_prepareForm();
    }
}

obviously the form will need some more attributes to work fully but I was just hoping to get some different attributes on to the form so I could make sure the block was being applied.

So at the moment when I go into the back end the form with the helper on it has no enctype still, and I am unsure if my helper block is even being loaded or whether it is and its just having no effect.

Am I going about this in the correct manner or not?

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

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

发布评论

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

评论(1

我是有多爱你 2024-11-03 19:08:32

表单应该像这样初始化:

protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
                                  'id' => 'edit_form',
                                  'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'),'store' => $this->getRequest()->getParam('store'))),
                                  'method' => 'post',
                                  'enctype' => 'multipart/form-data'
                               ));
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();
}

The form should be initialized like this:

protected function _prepareForm()
{
    $form = new Varien_Data_Form(array(
                                  'id' => 'edit_form',
                                  'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'),'store' => $this->getRequest()->getParam('store'))),
                                  'method' => 'post',
                                  'enctype' => 'multipart/form-data'
                               ));
    $form->setUseContainer(true);
    $this->setForm($form);
    return parent::_prepareForm();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文