1)给某种类型的节点 定义一个表单
function upload_jiangyi_form(&$form_state, $node) {
global $user;
$node = (object)$node;
$form['nid'] = array(
'#type' => 'hidden',
'#default_value' => $node->nid,
);
// Set the id of the top-level form tag
$form['#id'] = 'node-form';
// Basic node information.
// These elements are just values so they are not even sent to the client.
// Changed must be sent to the client, for later overwrite error checking.
$form['changed'] = array(
'#type' => 'hidden',
'#default_value' => isset($node->changed) ? $node->changed : NULL,
);
// Get the node-specific bits.
if (!isset($form['title']['#weight'])) {
$form['title']['#weight'] = -5;
}
$form['#node'] = $node;
// Add a log field if the "Create new revision" option is checked, or if the
// These values are used when the user has no administrator access.
foreach (array('uid', 'created') as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => $node->$key,
);
}
// Add the buttons.
$form['buttons'] = array();
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#access' => !variable_get('node_preview', 0) || (!form_get_errors() && isset($form_state['node_preview'])),
'#value' => t('Save'),
'#weight' => 5,
//'#submit' => array('node_form_submit'),
);
//$form['#validate'][] = 'node_form_validate'; //它使用的是原来的验证
$form['#theme'] = array($node->type .'_node_form', 'node_form');
return $form;
}
2)表单提交函数
//表单函数_submit 提交函数
function upload_jiangyi_form_submit(&$form, &$form_state)
{
/**
* 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
*/
global $user;
$node->files=array();
$fileObjs = array();
foreach($form_state['values']['files'] as $key=>$value){
$file = (object)$file;
$file->list = $value['list'];
$file->description = $value['description'];
$file->weight = $value['weight'];
$file->new = !empty($form['#node']->files[$fid]->new);
$form['#node']->files[$key] = (array)$file;
}
//var_dump($form['#node']->files);
node_save($form['#node']);
$node=node_load($form_state['values']['nid']);
// dsm($node); dpm($node);
drupal_set_message('上传讲义成功!');
}
3)最后好像有个tabledrag.js 使 weight隐藏了,我们把这个js移掉