How to set a magento widget form to enctype=“multipart/form-data” for file upload?

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?

One thought on “How to set a magento widget form to enctype=“multipart/form-data” for file upload?”

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

Leave a Reply

Your email address will not be published. Required fields are marked *