magento自定义模块,可上传多个图像

时间:2013-07-10 07:49:41

标签: magento-1.7

我想创建自定义模块,可以上传多个图像,例如product.I创建了一个自定义模块,但只上传了一个图像。

form.php的

$fieldset->addField('filename', 'image', array(
     'label'     => Mage::helper('footertop')->__('File'),
     'name'      => 'filename',

  ));

1 个答案:

答案 0 :(得分:2)

我认为您需要为图片字段创建自定义渲染器。为此,在您的模块中创建此类:

class [YOURNamespace]_[YOURModule]_Block_Adminhtml_[YOUREntity]_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', '[YOURNamespace]_[YOURModule]_Block_Adminhtml_[YOUREntity]_Helper_Image');

或者如果你想成为政治上正确的"这样添加:

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

这将告诉Magento,在您当前的fieldset中,所有带有type类型的字段都应该由您自己的类呈现。

现在,您可以像添加字段一样添加字段:

$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('YOURmodule')->__('Select Image'),
        'title'     => Mage::helper('YOURmodule')->__('Can select multiple images'),
        'required'  => true,
        'disabled'  => $isElementDisabled
    ));

那就是它。 不要忘记用您的值替换占位符([YOURModule]和其他)。

相关问题