使用 upload_element 模块 不知怎么搞的,使用upload_element 模块 对附件(upload 模块 好像不起作用,只好自己建个文件上传的字段吧)
下面是有用的代码
function accept_task_do_form(&$form_state,$accept_task_node)//accept_task_form 就是 $form_id
{
$content_field = content_fields('field_acce_task_status');
$allowed_values = content_allowed_values($content_field);
// $element = content_fields('field_image', 'accept_task_node_form');
// $form['#field_info']['field_image'] = $element;
// $element = (array) content_field_form($form, $form_state, $element);
// $form['field_image'] = $element['field_image'];
$form['nid'] = array(
'#type' =>'hidden',//
'#value'=>$accept_task_node->nid,
);
// $form['field_acce_task_pic'] = array(
// '#type' =>'file',//
// '#title'=>'图像',
//
// );
// $form['field_acce_task_pic'] = array(
// '#type' => 'image_upload_element',
// '#title'=>'图像',
//// /'#default_value' => $accept_task_node->field_acce_task_pic, // {files} object
// );
//好像 字段名字 非要有 fid 或 fid1 fid2 fid3 fid4 等等
$field_acce_task_pic_obj = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $accept_task_node->field_acce_task_pic[0]['fid']));
if(!empty($accept_task_node->field_acce_task_pic[0]))
{
$form['fid'] = array(
'#type' => 'image_upload_element',
'#title' => '图像',
'#required' => FALSE,
'#default_value' => $field_acce_task_pic_obj,//这是一个对象 不是数组
'#file_validators' => array('file_validate_size' => array(524288)),
'#image_preview_size' => '150x150',
);
}else{
$form['fid']= array(
'#type' => 'image_upload_element',
'#title' => '图像',
'#required' => FALSE,
'#file_validators' => array('file_validate_size' => array(524288)),
'#image_preview_size' => '150x150',
);
}
// 文本框 name是文本框的名称
$form['field_acce_task_link'] = array(
'#type' =>'textfield',//drupal 规定文本框用textfield来表示
'#title'=>'链接',
'#default_value' => $accept_task_node->field_acce_task_link[0]['url'],
'#size'=>20
);
$form['field_acce_task_status'] = array(
'#type' =>'select',//drupal 规定文本框用textfield来表示
'#default_value' => $accept_task_node->field_acce_task_status[0]['value'],
'#options' => $allowed_values ,
'#title'=>'状态',
);
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#attributes']['class'] = 'accept-task-do-form clearfix';
$form['msg']= array(
'#prefix'=>'<div id="user-dig-msg'.$accept_task_node->nid.'">',
'#type'=>'item',
'#suffix'=>'</div>',
);
//提交按钮 ////提交按钮 // 好像如果使用 ahah ajax 提交 这里不能为 submit
$form['submit1'] = array(
'#type'=>'submit', //记住 这里切切要是 submit 如果是button的话 就会无法 执行 form_submit 也无法执行 form_validate
'#value'=>'确定',
//这里 #submit 和下面的 $form['#submit'][] 好像作用是一样的 //反正好像不能使用原来的,好像去掉也可以
//'#submit'=>array('accept_task_do_form_submit_ahah'),
'#ahah' => array(
//'event' => 'click',
// This is the "magical path". Note that the parameter is an array of
// the parents of the form item of the wrapper div!
'path' => 'modify_accept_task',
'wrapper' => 'user-dig-msg'.$accept_task_node->nid,
),
);
//这个 validate 好像去掉也可以
//$form['#validate'][] = 'accept_task_do_form_validate_ahah';
//$form['#submit'][] = 'accept_task_do_form_submit_ahah';
$form['delete'] = array('#type'=>'submit','#value'=>'删除');
$form['#after_build'][] = '_remove_form_item_div';
// $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print.
// print $export;
return $form;
}
function shipingzhongcustomtask_menu() {
$items = array();
$items['my_accept_task/%node'] = array(
'title'=>'我领取的任务',
'page callback'=>'shipingzhongcustomtask_my_accept_tasks_page',
'page arguments' => array(1),
//'page arguments' => array(1,2),
//它的优先级 大于 url传来的参数我
//如果要默认值 就放在回调函数的形参上
//array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
'access arguments'=> array('view my task')
);
$items['modify_accept_task'] = array(
'title'=>'修改的任务',
'page callback'=>'shipingzhongcustomtask_modify_accept_task_page',
'page arguments' => array(1),
//'page arguments' => array(1,2),
//它的优先级 大于 url传来的参数我
//如果要默认值 就放在回调函数的形参上
//array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
'access arguments'=> array('view my task')
);
$items['delete_accept_task'] = array(
'type' => MENU_CALLBACK,
'title'=>'删除的任务',
'page callback'=>'shipingzhongcustomtask_delete_accept_task_page',
'page arguments' => array(1,2),
//'page arguments' => array(1,2),
//它的优先级 大于 url传来的参数我
//如果要默认值 就放在回调函数的形参上
//array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
'access arguments'=> array('delete custom accept task')
);
$items['complete_redo_accept_task'] = array(
'type' => MENU_CALLBACK,
'title'=>'完成或重做的任务',
'page callback'=>'shipingzhongcustomtask_complete_redo_accept_task',
//'page arguments' => array(1,2),
//它的优先级 大于 url传来的参数我
//如果要默认值 就放在回调函数的形参上
//array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
'access callback' => 'modify_accept_task_by_my_pub',
);
return $items;
}
function shipingzhongcustomtask_modify_accept_task_page()
{
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Process the form with drupal_process_form(), which calls the submit handlers that put whatever was worthy of keeping in the $form_state
drupal_process_form($form_id, $form, $form_state);
//进行上面的表单验单 提交操作的时候,好像用户的 $user->cache 这个时间戳就变了 就变成当前时间了
//同时上面的表单构建的时间 (是要比较$user->cache 时间和 $cache->created 时间的)
//所以为了另一个ajax提交 我们人为的修改下 $user->cache
global $user;
//用户缓存时间 减去 7200吧 (两个小时之内吧) 如果不这么做的话 第二个 ajax 表单 及第二次ajax 之后 表单就无法提交
$user->cache = $user->cache-7200;
if ($error = form_get_errors()){
$output = implode($error);
// since there were errors, drupal_process_form has done its job. No need to rebuild $form.
// Session messages would get displayed at the next regular request, but we're in AHAH here, so that won't happen. Make them go away.
unset($_SESSION['messages']);
}
else {
$content_field = content_fields('field_acce_task_status');
$allowed_values = content_allowed_values($content_field);
$field_acce_task_status = $form_state['values']['field_acce_task_status'];
$message = drupal_get_messages('status');
$output = '状态已更改为 <span class="hongse">'.$allowed_values[$field_acce_task_status].'</span>'; // you might want more than one status message
// $output = '状态已更改为 '.$allowed_values[$field_acce_task_status].''; // you might want more than one status message
// everything has been submitted in drupal_process_form, so $form has to be rebuilt
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
unset($_SESSION['messages']); // same as above
}
//好像这里不能用 一用的话 好像数据出错
//drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
drupal_json(array('status' => TRUE, 'data' => $output));
exit;
}
function shipingzhongcustomtask_delete_accept_task_page()
{
$master_nid = db_result(db_query("SELECT nid FROM `content_field_accept_task` where field_accept_task_nid='%d'", intval($_POST['nid'])));
$master_node = node_load($master_nid);
$master_node->field_remaining_num[0]['value'] = intval($master_node->field_remaining_num[0]['value'])+1;
//让主 发布任务node 保存
node_save($master_node);
//删除任务
node_delete(intval($_POST['nid']));
//好像这里不能用 一用的话 好像数据出错
//drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
//进行上面的 删除操作的时候,好像用户的 $user->cache 这个时间戳同样会变了 就变成当前时间了
//同时上面的表单构建的时间 (是要比较$user->cache 时间和 $cache->created 时间的)
//所以为了另一个ajax提交 我们人为的修改下 $user->cache
global $user;
//用户缓存时间 减去 7200吧 (两个小时之内吧) 如果不这么做的话 第二个 ajax 表单 及第二次ajax 之后 表单就无法提交
$user->cache = $user->cache-7200;
drupal_json(array('status' => TRUE, 'data' =>'已删除这个任务'));
}
下面是全部的代码
<?php
function shipingzhongcustomtask_nodeapi(&$node, $op, $a3 = null , $a4 = null){
if($op == 'presave'){
//这是对发布任务的hook
if($node->type == 'publish_task'){
//仅仅新增的时候 置剩余件数 等于 件数
if(empty($node->nid)){
$node->field_remaining_num[0]['value'] = $node->field_pub_task_num[0]['value'];
}
//在新增 修 修改之前 把 总金额置为 数量 乘以 单价
$node->field_pub_task_money[0]['value'] = floatval($node->field_pub_task_num[0]['value'])* floatval($node->field_pub_task_price[0]['value']);
// $tasks = $node->field_accept_task;
// //如果 接受的任务不存在的话 就删除这个接受任务的引用吧
// foreach($tasks as $key=>$value){
// $accept_task_node = node_load($value['nid']);
// if(empty($accept_task_node)){
// drupal_set_message($value['nid']);
// unset($tasks[$key]);
// }
// }
// $node->field_accept_task = $tasks;
}
}
}
function shipingzhongcustomtask_form_alter(&$form, &$form_state, $form_id)
{
if($form_id=='publish_task_node_form' )
{
$form['#after_build'][] = '_set_field_pub_task_money_readonly';
}
if($form_id=='accept_task_node_form' )
{
//$form['#after_build'][] = '_set_accept_task_node_form';
}
}
function _set_accept_task_node_form($form, &$form_state){
$export = kprint_r($form, TRUE); // $vars - is a variable which you want to print.
print $export;
return $form;
}
function _set_field_pub_task_money_readonly($form, &$form_state)
{
_my_fix_readonly($form['field_pub_task_money']);
return $form;
}
function _my_fix_readonly(&$elements){
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
// Recurse through all children elements.
_my_fix_readonly($elements[$key]);
}
}
if (!isset($elements['#attributes'])) {
$elements['#attributes'] = array();
}
$elements['#attributes']['readonly'] = 'readonly';
$elements['#attributes']['style'] = 'background:#ebebe4;border:1px solid #a9a9a9';
}
function _accept_task_num($publish_task_nid){
return drupal_get_form('accept_task_form',$publish_task_nid);
}
function accept_task_form(&$form_state,$publish_task_nid)//accept_task_form 就是 $form_id
{
$arg1 = arg(1);
$arg2 = arg(2);
if($arg1==$publish_task_nid){
$form['accept_task_num'] = array(
'#type' =>'textfield',//drupal 规定文本框用textfield来表示
'#size' => 2,
'#default_value' => $arg2
);
}else {
$form['accept_task_num'] = array(
'#type' =>'textfield',//drupal 规定文本框用textfield来表示
'#size' => 2,
);
}
// 文本框 name是文本框的名称
$form['publish_task_nid'] = array(
'#type' =>'hidden',//drupal 规定文本框用textfield来表示
'#default_value' => $publish_task_nid
);
//提交按钮 //
$form['submit'] = array('#type'=>'submit','#value'=>'领单');
$form['#after_build'][] = '_remove_form_item_div';
return $form;
}
function _remove_form_item_div($form, &$form_state)
{
//$form['#theme'] = array('accept_task_form');
if(!empty($form['accept_task_num'])){
$form['accept_task_num']['#theme'] = array('remove_form_item_div_for_field');
}
if(!empty($form['field_acce_task_pic'])){
$form['field_acce_task_pic']['#theme'] = array('remove_form_item_div_for_file');;
// $form['field_acce_task_pic']['#theme'] = array('content_multiple_values');;
}
if(!empty($form['fid'])){
//$form['fid']['#theme'] = array('remove_form_item_div_for_upload_file');;
}
if(!empty($form['field_acce_task_link'])){
$form['field_acce_task_link']['#theme'] = array('remove_form_item_div_for_field');
}
if(!empty($form['field_acce_task_status'])){
$form['field_acce_task_status']['#theme'] = array('remove_form_item_div_for_select');
}
//
// $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print.
// print $export;
return $form;
}
function shipingzhongcustomtask_theme() {
return array(
'accept_task_form' => array(
'render element' => 'form',
),
'remove_form_item_div_for_field' => array(
'render element' => 'element',
),
'remove_form_item_div_for_select' => array(
'render element' => 'element',
),
'remove_form_item_div_for_file' => array(
'render element' => 'element',
),
'remove_form_item_div_for_upload_file' => array(
'render element' => 'element',
),
'my_accept_task' =>array(
'template' => 'my_accept_task'
)
);
}
function theme_remove_form_item_div_for_upload_file($element) {
return 'aaaa';
}
function theme_remove_form_item_div_for_file($element)
{
_form_set_class($element, array('form-file'));
//return theme('form_element', $element, '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n");
return _accept_task_num_text_field_form_element($element, '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n");
}
function theme_remove_form_item_div_for_select($element)
{
$select = '';
$size = $element['#size'] ? ' size="'. $element['#size'] .'"' : '';
_form_set_class($element, array('form-select'));
$multiple = $element['#multiple'];
return _accept_task_num_text_field_form_element($element, '<select name="'. $element['#name'] .''. ($multiple ? '[]' : '') .'"'. ($multiple ? ' multiple="multiple" ' : '') . drupal_attributes($element['#attributes']) .' id="'. $element['#id'] .'" '. $size .'>'. form_select_options($element) .'</select>');
}
function theme_remove_form_item_div_for_field($element)
{
$size = empty($element['#size']) ? '' : ' size="'. $element['#size'] .'"';
$maxlength = empty($element['#maxlength']) ? '' : ' maxlength="'. $element['#maxlength'] .'"';
$class = array('form-text');
$extra = '';
$output = '';
if ($element['#autocomplete_path'] && menu_valid_path(array('link_path' => $element['#autocomplete_path']))) {
drupal_add_js('misc/autocomplete.js');
$class[] = 'form-autocomplete';
$extra = '<input class="autocomplete" type="hidden" id="'. $element['#id'] .'-autocomplete" value="'. check_url(url($element['#autocomplete_path'], array('absolute' => TRUE))) .'" disabled="disabled" />';
}
_form_set_class($element, $class);
if (isset($element['#field_prefix'])) {
$output .= '<span class="field-prefix">'. $element['#field_prefix'] .'</span> ';
}
$output .= '<input type="text"'. $maxlength .' name="'. $element['#name'] .'" id="'. $element['#id'] .'"'. $size .' value="'. check_plain($element['#value']) .'"'. drupal_attributes($element['#attributes']) .' />';
if (isset($element['#field_suffix'])) {
$output .= ' <span class="field-suffix">'. $element['#field_suffix'] .'</span>';
}
return _accept_task_num_text_field_form_element($element, $output) . $extra;
}
function _accept_task_num_text_field_form_element($element, $value)
{
// This is also used in the installer, pre-database setup.
$t = get_t();
$output = '';
$required = !empty($element['#required']) ? '<span class="form-required" title="'. $t('This field is required.') .'">*</span>' : '';
if (!empty($element['#title'])) {
$title = $element['#title'];
if (!empty($element['#id'])) {
$output .= ' <label for="'. $element['#id'] .'">'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
else {
$output .= ' <label>'. $t('!title: !required', array('!title' => filter_xss_admin($title), '!required' => $required)) ."</label>\n";
}
}
$output .= " $value\n";
if (!empty($element['#description'])) {
$output .= ' <div class="description">'. $element['#description'] ."</div>\n";
}
return $output;
}
function theme_accept_task_form($form) {
$output = '';
// $output .= drupal_render($element['form_id']);
// $output .= drupal_render($element['form_build_id']);
// $output .= drupal_render($element['form_token']);
// $output .= drupal_render($element['accept_task_num']);
// $output .= drupal_render($element['submit']);
//$output .= drupal_render_children($form); //这个好像 drupal 6 不行 drupal 7 才行
$output .= drupal_render($form);
return $output;
// Anonymous div to satisfy XHTML compliance.
// $action = $element['#action'] ? 'action="' . check_url($element['#action']) . '" ' : '';
// return '<form ' . $action . ' accept-charset="UTF-8" method="' . $element['#method'] . '" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . ">\n<div>" . $element['#children'] . "\n</div></form>\n";
}
//表单函数_validate 验证类似于钩子函数的回调函数
function accept_task_form_validate(&$form, &$form_state)
{
$publish_task_nid = $form_state['values']['publish_task_nid'];
$accept_task_num = intval($form_state['values']['accept_task_num']);
if(empty($accept_task_num) || !is_int($accept_task_num) || $accept_task_num <=0){
form_set_error('','领单件数必须为正整数');
// $form['accept_task_num']['#default_value'] = '';
// $form_state['values']['accept_task_num'] = '';
// form_set_value($form['accept_task_num'], '', $form_state);
drupal_goto('list-publish-task/'.$publish_task_nid.'/'.$accept_task_num);
exit;
}
$node_publish_task = node_load($publish_task_nid);
/**
* 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
*/
// form_set_error('',$form_values['name']);
if($accept_task_num > $node_publish_task->field_remaining_num[0]['value'])
{
form_set_error('','领单件数是 '.$accept_task_num.' 不能大于剩余件数 '.$node_publish_task->field_remaining_num[0]['value']);
// $form['accept_task_num']['#default_value'] = '';
// $form_state['values']['accept_task_num'] = '';
// form_set_value($form['accept_task_num'], '', $form_state);
drupal_goto('list-publish-task/'.$publish_task_nid.'/'.$accept_task_num);
exit;
}
// $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print.
// print $export;
// $export = kprint_r($form_state, TRUE); // $vars - is a variable which you want to print.
// print $export;
// var_dump($form_state['values']['publish_task_nid']);
// var_dump($form_state['values']['accept_task_num']);
// var_dump($node_publish_task->field_remaining_num[0]['value']);
// var_dump("AAAA");exit;
}
//表单函数_submit 提交函数
function accept_task_form_submit(&$form, &$form_state)
{
global $user;
$publish_task_nid = $form_state['values']['publish_task_nid'];
$node_publish_task = node_load($publish_task_nid);
$accept_task_num = intval($form_state['values']['accept_task_num']);
for($i=0;$i<$accept_task_num;$i++)
{
$node = null;
$node = (object)$node; //创建空节点对象
$node->uid = $user->uid; // 创建订单者统一设为超级管理员
$node->type = 'accept_task'; //节点类型为“领取任务”
$node->title = '接受'.$node_publish_task->title.date('YmdHis').'-'.rand(1,1000);
$node->field_acce_task_status[0]['value'] = 'nodo';
node_save($node);
// drupal_set_message($node->nid);
$node_publish_task->field_accept_task[]=array('nid'=>$node->nid);
}
$node_publish_task->field_remaining_num[0]['value'] = $node_publish_task->field_remaining_num[0]['value'] - $accept_task_num;
node_save($node_publish_task);
/**
* 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
*/
drupal_set_message('领单成功!');
drupal_goto('my_accept_task/'.$publish_task_nid);
}
function shipingzhongcustomtask_menu() {
$items = array();
$items['my_accept_task/%node'] = array(
'title'=>'我领取的任务',
'page callback'=>'shipingzhongcustomtask_my_accept_tasks_page',
'page arguments' => array(1),
//'page arguments' => array(1,2),
//它的优先级 大于 url传来的参数我
//如果要默认值 就放在回调函数的形参上
//array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
'access arguments'=> array('view my task')
);
$items['modify_accept_task'] = array(
'title'=>'修改的任务',
'page callback'=>'shipingzhongcustomtask_modify_accept_task_page',
'page arguments' => array(1),
//'page arguments' => array(1,2),
//它的优先级 大于 url传来的参数我
//如果要默认值 就放在回调函数的形参上
//array(1,2)表示只取url前两个参数 array(1)表示只取url第一个参数
'access arguments'=> array('view my task')
);
return $items;
}
function _custom_sort_status_order($a,$b)
{
if($a->field_acce_task_status[0]['value'] == $b->field_acce_task_status[0]['value']){
return 0;
}
$content_field = content_fields('field_acce_task_status');
$allowed_values = content_allowed_values($content_field);
$allowed_value_keys = array_keys($allowed_values);
$index_a = array_search($a->field_acce_task_status[0]['value'],$allowed_value_keys);
$index_b = array_search($b->field_acce_task_status[0]['value'],$allowed_value_keys);
return ($index_a<$index_b)?-1:1;
}
//按未做 重做 已做 完成 进行排序 redo nodo havedo completedo
function _sort_status_order($arr)
{
uasort($arr,"_custom_sort_status_order");
return $arr;
}
function shipingzhongcustomtask_my_accept_tasks_page($node_publish_task=null)
{
global $user;
if(empty($node_publish_task)){
return '';
}
$node_publish_task = node_build_content($node_publish_task);
$tasks = $node_publish_task->field_accept_task;
//如果 接受的任务不存在的话 就删除这个接受任务的引用吧
$my_tasks = array();
foreach($tasks as $key=>$value){
$accept_task_node = node_load($value['nid']);
if($accept_task_node->uid == $user->uid ){
$my_tasks[$accept_task_node->nid] = $accept_task_node;
}
}
$my_tasks = _sort_status_order($my_tasks);
$output_string = '';
//$content = field_view_field('node', $node_publish_task, 'field_pub_task_type');
$output_string .= "任务的名称:".$node_publish_task->title;
$content_field = content_fields('field_pub_task_type');
$allowed_values = content_allowed_values($content_field);
$output_string .=' ';
$output_string .= "任务的类型:".$allowed_values[$node_publish_task->field_pub_task_type['0']['value']];
$output_string .='<br />';
foreach($my_tasks as $key=>$accept_task_node){
$output_string .= drupal_get_form('accept_task_do_form', $accept_task_node);
}
//$output_string .= theme('my_accept_task');
return $output_string;
}
function accept_task_do_form(&$form_state,$accept_task_node)//accept_task_form 就是 $form_id
{
$content_field = content_fields('field_acce_task_status');
$allowed_values = content_allowed_values($content_field);
// $element = content_fields('field_image', 'accept_task_node_form');
// $form['#field_info']['field_image'] = $element;
// $element = (array) content_field_form($form, $form_state, $element);
// $form['field_image'] = $element['field_image'];
$form['nid'] = array(
'#type' =>'hidden',//
'#value'=>$accept_task_node->nid,
);
// $form['field_acce_task_pic'] = array(
// '#type' =>'file',//
// '#title'=>'图像',
//
// );
// $form['field_acce_task_pic'] = array(
// '#type' => 'image_upload_element',
// '#title'=>'图像',
//// /'#default_value' => $accept_task_node->field_acce_task_pic, // {files} object
// );
//好像 字段名字 非要有 fid 或 fid1 fid2 fid3 fid4 等等
$field_acce_task_pic_obj = db_fetch_object(db_query("SELECT * FROM {files} WHERE fid = %d", $accept_task_node->field_acce_task_pic[0]['fid']));
if(!empty($accept_task_node->field_acce_task_pic[0]))
{
$form['fid'] = array(
'#type' => 'image_upload_element',
'#title' => '图像',
'#required' => FALSE,
'#default_value' => $field_acce_task_pic_obj,//这是一个对象 不是数组
'#file_validators' => array('file_validate_size' => array(524288)),
'#image_preview_size' => '150x150',
);
}else{
$form['fid']= array(
'#type' => 'image_upload_element',
'#title' => '图像',
'#required' => FALSE,
'#file_validators' => array('file_validate_size' => array(524288)),
'#image_preview_size' => '150x150',
);
}
// 文本框 name是文本框的名称
$form['field_acce_task_link'] = array(
'#type' =>'textfield',//drupal 规定文本框用textfield来表示
'#title'=>'链接',
'#default_value' => $accept_task_node->field_acce_task_link[0]['url'],
'#size'=>20
);
$form['field_acce_task_status'] = array(
'#type' =>'select',//drupal 规定文本框用textfield来表示
'#default_value' => $accept_task_node->field_acce_task_status[0]['value'],
'#options' => $allowed_values ,
'#title'=>'状态',
);
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#attributes']['class'] = 'accept-task-do-form clearfix';
$form['msg']= array(
'#prefix'=>'<div id="user-dig-msg'.$accept_task_node->nid.'">',
'#type'=>'item',
'#suffix'=>'</div>',
);
//提交按钮 // 好像如果使用 ahah ajax 提交 这里不能为 submit
$form['submit1'] = array(
'#type'=>'submit', //记住 这里切切要是 submit 如果是button的话 就会无法 执行 form_submit 也无法执行 form_validate
'#value'=>'确定',
//这里 #submit 和下面的 $form['#submit'][] 好像作用是一样的 //反正好像不能使用原来的,好像去掉也可以
//'#submit'=>array('accept_task_do_form_submit_ahah'),
'#ahah' => array(
//'event' => 'click',
// This is the "magical path". Note that the parameter is an array of
// the parents of the form item of the wrapper div!
'path' => 'modify_accept_task',
'wrapper' => 'user-dig-msg'.$accept_task_node->nid,
),
);
//这个 validate 好像去掉也可以
//$form['#validate'][] = 'accept_task_do_form_validate_ahah';
//$form['#submit'][] = 'accept_task_do_form_submit_ahah';
$form['delete'] = array('#type'=>'submit','#value'=>'删除');
$form['#after_build'][] = '_remove_form_item_div';
// $export = kprint_r($form, TRUE); // $vars - is a variable which you want to print.
// print $export;
return $form;
}
function shipingzhongcustomtask_image($image = FALSE, $alt = FALSE, $title = FALSE, $copy = FALSE) {
if (is_object($image)) {
return theme('imagecache', 'upload_element_preview', $image->filepath, $alt, $title) . '<br/><i>' . check_plain($copy) . '</li>';
}
return '--';
}
function _check_url($url){
if(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%
=~_|]/i",$url)){
return false;
}
return true;
}
function accept_task_do_form_validate($form, &$form_state) {
if(!empty($form_state['values']['field_acce_task_link'])){
if(!_check_url($form_state['values']['field_acce_task_link'])){
form_set_error('field_acce_task_link', '链接网址格式不对!');
}
}
}
function accept_task_do_form_validate_ahah($form, &$form_state) {
//form_set_error('field_acce_task_link', '链接网址格式不对_validate_ahah!');
accept_task_do_form_validate($form, $form_state);
}
function accept_task_do_form_submit($form, &$form_state) {
global $user;
$nid = $form_state['values']['nid'];
$fid = $form_state['values']['fid']->fid;
$field_acce_task_link = $form_state['values']['field_acce_task_link'];
$field_acce_task_status = $form_state['values']['field_acce_task_status'];
$node = node_load($nid);
if(!empty($fid)){
$fid_obj = $form_state['values']['fid'];
// upload_element_save($fid_obj, date('Y').'/'.date('m').'/'.date('d').'/'.$user->uid.'/', FILE_EXISTS_RENAME,'imagecache_preset');
// upload_element_save($fid_obj, 'dest/directory', FILE_EXISTS_RENAME);
//好像不能创建二层目录
upload_element_save($fid_obj, 'upload_element/'.date('Y-m-d').'-'.$user->uid, FILE_EXISTS_RENAME);
$node->field_acce_task_pic[0]['fid'] = $fid;
}
if(!empty($field_acce_task_link)){
$node->field_acce_task_link[0]['url'] = $field_acce_task_link;
}
if(!empty($field_acce_task_status)){
$node->field_acce_task_status[0]['value'] = $field_acce_task_status;
}
node_save($node);
$content_field = content_fields('field_acce_task_status');
$allowed_values = content_allowed_values($content_field);
drupal_set_message('任务状态更改为 '.$allowed_values[$field_acce_task_status]);
}
function accept_task_do_form_submit_ahah($form, &$form_state) {
//form_set_error('field_acce_task_link', '链接网址格式不对_submit_ahah!');
accept_task_do_form_submit($form, $form_state);
}
function shipingzhongcustomtask_modify_accept_task_page()
{
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Process the form with drupal_process_form(), which calls the submit handlers that put whatever was worthy of keeping in the $form_state
drupal_process_form($form_id, $form, $form_state);
if ($error = form_get_errors()){
$output = implode($error);
// since there were errors, drupal_process_form has done its job. No need to rebuild $form.
// Session messages would get displayed at the next regular request, but we're in AHAH here, so that won't happen. Make them go away.
unset($_SESSION['messages']);
}
else {
$content_field = content_fields('field_acce_task_status');
$allowed_values = content_allowed_values($content_field);
$field_acce_task_status = $form_state['values']['field_acce_task_status'];
$message = drupal_get_messages('status');
$field_acce_task_status =
$output = '状态已更改为 '.$allowed_values[$field_acce_task_status]; // you might want more than one status message
// everything has been submitted in drupal_process_form, so $form has to be rebuilt
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
unset($_SESSION['messages']); // same as above
}
drupal_json(array('status' => TRUE, 'data' => $output));
}
HOWTO: Submit a Form with AHAH in Drupal 6
Submitted by cwernli on Sun, 07/11/2010 - 11:49Dynamic form extension and validation with AHAH is quite easy in Drupal 6; form submission via AHAH on the other hand is less trivial. If you're familiar with AHAH Dynamic Form Extension then this article should fill in the missing details - this example deals with submitting a custom content type:
Step 1: Set up callbacks for button click and (optionally) form validation:
function MYMODULE_form_alter (&$form, $form_state, $form_id){
switch ($form_id) {
case 'MYFORM_node_form':
$form['buttons']['submit']['#ahah'] = array(
'event' => 'click',
'path' => 'MYCALLBACK/submit',
'wrapper' => 'submit-feedback'
);
// 好像 如果是 ahah 所有元素的 type为 submit时 就调用默认的表单验证 换句话说 下面这一行 可以注掉,见上面自己亲自做的 就知道了
$form['#validate'][] = 'MYFORM_validation';
break;
}
}
Step 2: Set up menu callback and point it to MYMODULE_submit (described elsewhere).
Step 3: Handle form submission in MYMODULE_submit:
function MYMODULE_submit() {
// include node definition
module_load_include('inc', 'node', 'node.pages');
// load form
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Process the form with drupal_process_form(), which calls the submit handlers that put whatever was worthy of keeping in the $form_state
drupal_process_form($form_id, $form, $form_state);
if ($error = form_get_errors()){
$output = implode($error);
// since there were errors, drupal_process_form has done its job. No need to rebuild $form.
// Session messages would get displayed at the next regular request, but we're in AHAH here, so that won't happen. Make them go away.
unset($_SESSION['messages']);
}
else {
$message = drupal_get_messages('status');
$output = $message['status']['0']; // you might want more than one status message
// everything has been submitted in drupal_process_form, so $form has to be rebuilt
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
unset($_SESSION['messages']); // same as above
}
drupal_json(array('status' => TRUE, 'data' => $output));
}
Step 4: Optionally handle validation in MYFORM_validation:
function MYFORM_validation(&$form, &$form_state) {
if ($form_state['values']['SOMEFIELD'] == '') {
form_set_error('', t('SOMEFIELD must not be empty.'));
}
}
来自 http://christoph.wernli.org/fact/howto/howto-submit-form-ahah-drupal-6
So there has been a lot of blogging and documentation in the past about the "right" way to do AHAH in Drupal. I think these people make excellent points, provide good documentation, and are pushing these types of dynamic interactions in Drupal in the right direction. I think a lot what's out there about this "right way" to do AHAH in Drupal misses some critical issues, though. Maybe someone else has blogged about it already, but I'll share one pitfall that I learned to avoid while doing AHAH.
Specifically, if you have a form using AHAH, there are a series of events that can lead to the form breaking completely:
Load form
Activate AHAH element (which calls AHAH callback, rebuilds form, then re-caches form)
Submit form with an error
Validation reloads the page with a re-rendered copy of the form and errors
Fix form errors, and submit form
BOOM!
What happened? The form submits and you probably get a load of JSON output, and your browser's URL is at the AHAH callback location. WHAT?
So, what happened is this: during the AHAH callback, the form is rebuilt. However, during the form rebuild, the #action element on the form is set to the default... which is the current request URI, which will be the path to the AHAH callback. This is bad, because now when you have a form validation error and the form is re-rendered in its entirety (as opposed to just using a section of re-rendered form as during the AHAH callback), the form now has its #action property set to the path of the AHAH callback.
That is bad. There have been mentions of how to fix it, but here is how I did it:
So there is this semi-utility-function-like block of code that AHAH the "right way" depends on existing in the AHAH callback, which looks something like this:
// From http://drupal.org/node/331941
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
That final call to drupal_rebuild_form() does a couple things: it rebuilds the form array, and it caches the result. This means that we can't just change the $form variable here after our call to drupal_rebuild_form() -- we need to make our form generator function smart! To do that, I made an important change to the above code:
// From http://drupal.org/node/331941
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// Stash original form action to avoid overwriting with drupal_rebuild_form().
$form_state['action'] = $form['#action'];
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
Notice that I stashed the form's original #action property (as loaded from the previously-cached version of the form) in the $form_state variable. Then, when drupal_rebuild_form() goes to work, this little bit of code in my form function makes sure that my #action is correct through AHAH+Validation:
$form['#cache'] = TRUE; // Make sure the form is cached.
// Pull the correct action out of form_state if it's there to avoid AHAH+Validation action-rewrite.
if (isset($form_state['action'])) {
$form['#action'] = $form_state['action'];
}
Now when the form is rebuilt during my AHAH call, it explicitly sets the #action property, which prevents the #action overwrite when drupal_prepare_form() (which is called by drupal_rebuild_form()) adds the result of_element_info('form') to the form (adding an array to another will add keys from the second array to the first, but not overwrite keys in the first that are also present in the second -- so $form['#action'] being present already prevents it from being overwritten).