Jac*_*ris 5 php magento magento-1.7
我已经创建了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',
));
}
Run Code Online (Sandbox Code Playgroud)

我需要帮助才能在我的自定义模块中拥有多个图像上传器.
您需要为图像字段创建自定义渲染器.为此,在您的模块中创建此类:
<?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'));
}
}
Run Code Online (Sandbox Code Playgroud)
现在位于您_prepareForm(添加字段的位置)的顶部,在添加任何字段之前添加此行:
$fieldset->addType('image', '[Namespace]_[Module]_Block_Adminhtml_[Entity]_Helper_Image');
Run Code Online (Sandbox Code Playgroud)
或者如果你想要"政治正确",可以这样添加:
$fieldset->addType('image', Mage::getConfig()->getBlockClassName('[module]/adminhtml_[entity]_helper_image'));
Run Code Online (Sandbox Code Playgroud)
这将告诉Magento在您当前的fieldset中,所有具有类型的字段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,
));
Run Code Online (Sandbox Code Playgroud)
而已.
不要忘记[Module]用您的值替换占位符(和其他).
这基本上是覆盖/添加所需输入类型的方法.创建自己的类,应该扩展原始输入类(或者Varien_Data_Form_Element_Abstract如果添加一个新类)并在顶部声明它_prepareForm