I've tried and appear to be able to successfully create a simple 'page' node with the following code:

  $newnode = new stdClass();
  $newnode->title = 'programmatically created!';
  $newnode->body = 'This node was created by code!';
 
  global $user;
 
  $newnode->uid = $user->uid;
  $newnode->type = 'page';
  $newnode->status = 1;
  $newnode->promote = 0;
  node_save( $newnode ); 

Is this the correct way to do this under Drupal 6?

Investigating programmatically creating nodes, all but one example I've located demonstrate programmatically creating nodes with CCK fields, which I don't care about, and they are loose in their discussions about the offered examples being for whatever version of Drupal (4, 5, 6...) Hence the reason I ask if this is the correct logic for a simple 'page' in D6.

Also, what would I need to change to make the new node use PHP in it's body?

-Blake

Comments

dman’s picture

Short answer - yes, that's a good start, should work. It's been pretty standard throughout versions.
Long answer, every feature or widget beyond that basic will need looking at individually, but they are usually easy as.

So, GET devel and that will provide a button that will dump the PHP values for any node. Look at that, and emulate the bits you want to see.
By doing that - on a node that has the php input format flag set - you will see what needs to be done!

Longer answer - if you start using nodes that have more logic and widgets than the title and body, you may find it necessary to start using drupal_execute - as that emulates the submission of a form, and all the extra magic that may happen at that time. In practice, I've so far found just node_save($node) to be fine.

nevets’s picture

You may want to call node_submit() ($newnode = node_submit($newnode);) before calling node_save(). This allows other modules that implement hook_nodeapi() to do their thing (this includes taxonomy).

bsenftner’s picture

So others can get a worked solution easy:

  // let's create the node:
  $newnode = new stdClass();
  $newnode->title = 'title';
  $newnode->body = 'whatever php logic you like';
 
  global $user;
 
  $newnode->uid = $user->uid;
  $newnode->name = $user->name;
  $newnode->type = 'page';
  $newNode->format = 3;     // 1 means filtered html, 2 means full html, 3 is php
  $newnode->status = 1;     // 1 means published
  $newnode->promote = 0;
  $newnode = node_submit( $newnode );
  node_save( $newnode ); 

not much different from earlier, just now format is set and node_submit() was added.

Also, for others to see another working alternative (I've not tried yet, but seems really easy), here's an example using drupal_execute(), able to simulate the user filling out forms:
http://drupal.org/node/293663

dman’s picture

Looks good, why don't you copy that to the handbook? I didn't see see this example in the docs when I went looking.

karimahmed’s picture

Is there a way of extending this to performing field validation to prevent corrupt nodes in the DB?

bsenftner’s picture

The Forms API has rich user validation capabilities. The above would be used as a last step after such validation, when creating (for example) a summary node from some survey or software analysis. The idea being to use the above when you have complete control over the content.

wdmartin’s picture

Note that if you're using drupal_execute, you need to supply a duplicate copy of the node array as an object. That threw me for a loop when I tried to do it. See my full writeup at Programmatically creating nodes with drupal_execute.