我创建了adminhtml模块,它工作正常。在创建新项目表单时,有4个字段名称,图像,网址和电子邮件ID;
我使用文件上传器上传图片。它工作正常,但我无法上传多个图像。
是否可以拥有多个图像上传器?这是我的简单图像上传代码。
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',
));
}
我需要帮助才能在我的自定义模块中安装多个图片上传器。
答案 0 :(得分:8)
您需要为图片字段创建自定义渲染器。为此,在您的模块中创建此类:
<?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
(您添加字段的位置)的顶部,在添加任何字段之前添加此行:
$fieldset->addType('image', '[Namespace]_[Module]_Block_Adminhtml_[Entity]_Helper_Image');
或者,如果你想“在政治上正确”,可以这样添加:
$fieldset->addType('image', Mage::getConfig()->getBlockClassName('[module]/adminhtml_[entity]_helper_image'));
这将告诉Magento,在您当前的字段集中,所有类型为image
的字段都应由您自己的类呈现。
现在,您可以像添加字段一样添加字段:
$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]
和其他)。
这基本上是覆盖/添加所需输入类型的方法。创建自己的类,该类应该扩展原始输入类(如果添加新的输入类,则为Varien_Data_Form_Element_Abstract
),并在_prepareForm
的顶部声明它
答案 1 :(得分:1)
我在我的网络应用程序中使用了这段代码,试试吧。
$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
));
答案 2 :(得分:0)
如果您愿意,我可以免费下载该模块。
这是展览。