Magento自定义模块:多个adminhtml格式的图片上传器 [英] Magento Custom Module: multiple Image Uploader in adminhtml form
问题描述
我已经创建了adminhtml模块,它运行正常.在创建新商品的表单中,有4个字段名称,图片,网址和电子邮件ID;
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.
推荐答案
您需要为image字段创建自定义渲染器.为此,请在您的模块中创建此类:
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'));
}
}
现在在_prepareForm
(添加字段的位置)顶部,在添加任何字段之前添加以下行:
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'));
这将告诉Magento,在您当前的字段集中,所有类型为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,
));
就是这样.
不要忘记用您的值替换占位符([Module]
和其他).
That's it.
Don't forget to replace the placeholders ([Module]
and others) with your values.
基本上,这是重写/添加所需任何输入类型的方法.创建您自己的类,该类应扩展原始输入类(如果添加新输入类,则扩展为Varien_Data_Form_Element_Abstract
),并在_prepareForm
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
这篇关于Magento自定义模块:多个adminhtml格式的图片上传器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!