Magento Custom Module: multiple Image Uploader in adminhtml form

℡╲_俬逩灬. 提交于 2019-12-04 12:24:51

问题


i have created adminhtml module it is working fine. in create new item form there are 4 field name , images, url and email id;

i have used file uploader to upload images . it is working fine but i am not able to upload multiple images.

is it possible to have multiple image uploader? here is my simple image uploader code.

  if(isset($data['image']) && $data['image'] != ''){
        $finderLink = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) .'finder/store_locator/'.$data['image'];
        $finderName = $data['image'];
        $fieldset->addField('imagelabel', 'label', array(
            'label' => Mage::helper('finder')->__('Location Image'),
            'name'  =>'image',
            'value'     => $finderLink,
            'after_element_html' => '<img src="'.$finderLink .'" alt=" '. $finderName .'" height="120" width="120" />',
        ));

        $fieldset->addField('image', 'image', array(
            'label' => Mage::helper('finder')->__('Change image'),
            'required' => false,
            'name' => 'image',
        ));

    }else{
        $fieldset->addField('image', 'image', array(
            'label' => Mage::helper('finder')->__('image'),
            'required' => false,
            'name' => 'image',
        ));
    }

I need help to have multiple image uploader in my custom module.


回答1:


You need to create your custom renderer for the image field. For this create this class in your module:

<?php 
class [Namespace]_[Module]_Block_Adminhtml_[Entity]_Helper_Image extends Varien_Data_Form_Element_Image{
    //make your renderer allow "multiple" attribute
    public function getHtmlAttributes(){
        return array_merge(parent::getHtmlAttributes(), array('multiple'));
    }
}

Now at the top of your _prepareForm (where you add your fields) add this line before adding any field:

$fieldset->addType('image', '[Namespace]_[Module]_Block_Adminhtml_[Entity]_Helper_Image');

Or if you want to be "politically correct" add it this way:

$fieldset->addType('image', Mage::getConfig()->getBlockClassName('[module]/adminhtml_[entity]_helper_image'));

This will tell Magento that in your current fieldset, all the fields with type image should be rendered by your own class.

Now you can add your field like similar to how you did it:

$fieldset->addField('image', 'image', array(
            'name'      => 'image[]', //declare this as array. Otherwise only one image will be uploaded
            'multiple'  => 'multiple', //declare input as 'multiple'
            'label'     => Mage::helper('helper_alias')->__('Image'),
            'title'     => Mage::helper('helper_alias')->__('Image'),
            'required'  => true,
        ));

That's it.
Don't forget to replace the placeholders ([Module] and others) with your values.

This is basically the way to override/add any input type you want. Create your own class that should extend the original input class (or Varien_Data_Form_Element_Abstract if you add a new one) and declare it at the top of _prepareForm




回答2:


I have used this code in my web application, try it.

$fieldset->addField('image', 'image', array(
            'name'      => 'image',
            'multiple'  => 'multiple',
            'mulitple'  => true,
            'label'     => Mage::helper('magentostudy_design')->__('design Image'),
            'title'     => Mage::helper('magentostudy_design')->__('design Image'),
            'required'  => true,
            'disabled'  => $isElementDisabled
        ));



回答3:


i'd have the module that you can download it for free if you wanted.

this is the exhibit.



来源:https://stackoverflow.com/questions/19223886/magento-custom-module-multiple-image-uploader-in-adminhtml-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!