Recipe for adding Drupal node records

2019-06-24 00:08发布

问题:

I am looking for a recipe for adding Drupal node records. I have identified three tables.

node_revisions
nid=249  - vid + 1?
vid=248  - auto-increment

node:
nid=250  - vid + 1?
vid=249  - auto-increment

content_type_my_content
vid=248  - from node_revisions table?
nid=249  - from node table? 

Am I on right track? Is there some helper functions for this?

回答1:

If you are looking to programatically create nodes, use the Drupal API.

Start by creating a $node object. Fill in title, type, status, body, plus any CCK fields. At the end, call node_save($node);.

node_save will save your node object and do the necessary database work.

Check this out:

http://api.drupal.org/api/function/node_save/6

http://mediumexposure.com/how-build-node-drupal-programmatically/

The easiest way to see what each type of content type has as fields is to create a node (for example, Page), then use var_dump() to see the node's contents. That will show you every field you will need to use in your node object creation script.

Some folks will say you should create a form array, and call drupal_execute() on it so validation is performed before it's saved to the database. Either way is fine.



回答2:

Kevin - With your help I have made good progress. Node and CCK fields are now being populated.

Location (long/lat) is populated but not showing up on View screen. Checkboxes are not being populated.

global $user;
$newnode = new stdClass();
$newnode->title = 'New node title';
$newnode->body = "this is a new node, created by import function";
$newnode->uid = $user->uid;
$newnode->type = 'items';
$newnode->status = 1;
$newnode->promote = 0;

// CCK fields
$newnode->field_myfield1[0]['value'] = 'test 1';
$newnode->field_myfield2[0]['value'] = 'test 2';
$newnode->field_mycheckbox[0]['value'] =  1;

// longitude, lalitude
// $newnode->locations[0]['lid'] = ?;
$newnode->locations[0]['street'] = 'xx';
$newnode->locations[0]['city']   = 'xx';
$newnode->locations[0]['province']  = 'xx'; 
$newnode->locations[0]['postal_code']  = 'xx'; 
$newnode->locations[0]['latitude']  = 0; 
$newnode->locations[0]['longitude'] = 0; 


$newnode = node_submit($newnode);
node_save($newnode);
content_insert($newnode); 


回答3:

OK. here is the full recipe. Drupal does the rest automagically.

global $user;
// Node fields
$newnode = new stdClass();
$newnode->title =  $data[0];
$newnode->body =  $data[1];
$newnode->uid = $user->uid;
$newnode->type = 'mytype';
$newnode->status = 1;
$newnode->promote = 0;

// CCK fields
$newnode->field_myfield1[0]['value'] =  $something;
$newnode->field_myfield2[0]['value'] =   $something;
$newnode->field_my_checkbox[0]['value'] =  $something;

// longitude, latitude
$newnode->field_loc_latitude[0]['street'] =  $something; 
$newnode->field_loc_latitude[0]['city'] =   $something;
$newnode->field_loc_latitude[0]['province'] =  $something;
$newnode->field_loc_latitude[0]['postal_code'] =  $something;
$newnode->field_loc_latitude[0]['latitude'] =  '';
$newnode->field_loc_latitude[0]['longitude'] =  '';

$newnode = node_submit($newnode);
node_save($newnode);
content_insert($newnode);