Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recipe for adding Drupal node records

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?

like image 502
bert Avatar asked Feb 27 '23 07:02

bert


2 Answers

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.

like image 102
Kevin Avatar answered Mar 01 '23 19:03

Kevin


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); 
like image 43
bert Avatar answered Mar 01 '23 19:03

bert