在开发图片轮播模块模块时,想直接在模块中上传图片,起先的思路想做一个类似于upload模块的功能,利用drupal ahah功能上传文件,但考虑到可能会使用已有图片,决定使用IMCE模块的文件上传功能.
先到IMCE模块的主页去找一下IMCE的API功能,很简单的一个说明
- 要有一个text的字段
<input type="text" name="urlField" id="urlField">其中ID必须要有,
- 要有一个按钮
<input type="button" value="Browse" onclick="openFileBrowser()">用来打开IMCE文件管理器,而其中的openFileBrowser()是个JS函数内容如下
function openFileBrowser() {
window.open('/?q=imce&app=myApp|url@urlField', '', 'width=760,height=560,resizable=1');
}
这样就可使用IMCE文件浏览器上传文件.
但在表单中处理时却出现问题,因为在drupal中button类型也是经提交处理,和submit相类似,在drupal官网上找了N久也没看到一个处理方案,后来在一篇文章中提到可用默认的markup类型来处理.我是这样来处理的.
这是处理通过IMCE处理之后得到的文件路径和文件名,加了AHAH功能
$form[$i]['item_image']= array(
'#type' => 'textfield',
// '#title' => t('图片的路径'),
//'#size'=>20,
'#default_value' => variable_get('item'.$i.'_image',""),
// '#description' => t('图片的路径'),
'#id'=>'urlField'.$i,
'#ahah' => array(
'event' => 'blur',
'path' => 'tableform/js/'.$i,
'wrapper' => 'myform-wrapper'.$i,
'method' => 'replace',
'effect' => 'fade',
'progress' => array(
'type' => 'throbber',
'message' => t('Loading...')
)
),
);
//下面是处理IMCE上传部分
if(module_exists('imce') ){
$aa="window.open('/?q=imce&app=flashshow|url@urlField$i', '', 'width=760,height=560,resizable=1');$('#urlField$i').blur()";
$markup = '<input type="button" value="浏览图片" onclick="'.$aa.'" />';
$form[$i]['imceupload'] = array(
'#type' => 'markup',
'#value' => $markup
);
}else{
$markup = '你的drupal没有安装imce模块,不支持浏览图片,请直接填写图片路径';
$form[$i]['imceupload'] = array(
'#type' => 'markup',
'#value' => $markup
);
}
OK这样就能通过IMCE文件浏览已有文件,或直接上传处理.
最新评论