从网上参考的一篇文章。
1. node 基本字段
$node = new stdClass();
//Main Node Fields
$node->name = "Title";
$node->title = $node->name;
$node->body = "Lorem Ipsum";
$node->type = 'page'; //This can be any node type
$node->created = time();
$node->changed = $node->created;
$node->promote = 0; // Display on front page ? 1 : 0
$node->sticky = 0; // Display top of page ? 1 : 0
$node->format = 2; // 1:Filtered HTML, 2: Full HTML
$node->status = 1 // Published ? 1 : 0
$node->language = 'en';
2. CCK 字段
$node = new stdClass();
//Main Node Fields
/* This is a standard CCK text field. Note the array fields here
* If this field is allowed to have more than one entry, you can
* increment the "0" value to the maximum allowed number.
* Also, note how you must always populate the "value" instance
* of the array
*/
$node->my_text_field[0]['value'] = $data['IMAGE_CREDIT'];
/* This is a CCK textarea field.
* format of the textfield to be Filtered HTML, Full HTML
*/
$node->my_text_area[0]['value'] = $data['OFFLINE_CONTENT'];
$node->my_text_area[0]['format'] = 2;
3. Node reference 字段
$node->my_node_reference_field[0]['nid'] = $node_ref_id;
4. CCK time 字段
$node->my_time_field[0]['hour'] = $hour;
$node->my_time_field[0]['minute'] = $minute;
$node->my_time_field[0]['meridiem'] = $meridiem;
5. CCK file 字段
File 字段也是一个普通字段,默认值就是一个$file数组而已。
$file1 = field_file_load(53);
$file2 = field_file_load(52);
$node->field_up_image[] = $file1;
$node->field_up_image[] = $file2;
如果文件需要上传,那么先调用drupal 的 file_save_upload函数,把文件对象上传到服务器上,然后再赋值给 node。
另外下面的代码是把已存在的文件(drupal 未管理的文件)给 node 赋值。
//Inside your node creation function
$file = new stdClass();
$file->filepath = "relative-drupal-path-dir/".$filepath;
$node->field_event_speaker_image = array(drupal_add_existing_file($file->filepath));
//Separate function
function drupal_add_existing_file($file_drupal_path,$uid=1,$status=FILE_STATUS_PERMANENT) {
$file=(object)array(
'filename' =>basename($file_drupal_path),
'filepath' =>$file_drupal_path,
'filemime' =>file_get_mimetype($file_drupal_path),
'filesize' =>filesize("/var/www/clients/usni/htdocs/".$file_drupal_path),
'uid' =>$uid,
'status' =>$status,
'timestamp'=>time()
);
drupal_write_record('files',$file);
return field_file_load($file_drupal_path);
}