Drupal 7 – Programatically Create Nodes
Getting straight to the topic,
From drupal 6, there have been some small changes in the way users can create a node programatically. Like for eg. instead of using the drupal_execute function, we now have to use drupal_form_submit. Both these functions do the same thing, i.e, submit the form programatically.
Since, I experimented with creating book nodes, i’ll be running through the same example.
Minimally, 3 arguments need to be specified to the drupal_form_submit function in this case.
The first one, is the form id. This id can be got by taking a quick look at the form POST parameters while creating a book “normally”. I did that by using firebug on the node/add/book page.
The 2nd argument is an array of form values that should mimic the values posted from the browser. The focus should be to copy the (relevant) variable names that are seen while analyzing the post request. See below,

The values should go within an array keyed by the key ‘values’
global $user;
// Body Structure
// For more information on how this variable is read by the form engine, see
// drupal_array_get_nested_value.
// Substitute the language "en" for the language of the node. Make sure that
// the appropriate locale is enabled
$body["en"][0]['value'] = "Some body text";
$formState['values']['title'] = "Title of the book"
$formState['values']['language'] = "en"; // substitute for the language of the node
$formState['values']['name'] = $user->name;
$formState['values']['body'] = $body;
$formState['values']['status'] = 1;
$formState['values']['promote'] = 0;
$formState['values']['sticky'] = 0;
$formState['values']['op'] = t('Save');
And the last argument is a node object reference that one must create with the necessary information.
$newNode = array( 'type' => 'book', 'book' => array( 'bid' => $node->book['bid'], // use 'new' here if the book page is of a new book 'plid' => $node->book['mlid'], 'weight' => $chapNo, 'options' => array(), 'has_children' => 0, 'module' => 'book' ), 'language' => $node->language // This is needed );
Last thing to do is to call the drupal_form_submit function.
drupal_form_submit('book_node_form', $formState, (object) $newNode);
Thats it! We’re done!


Is there a way to generate the form_build_id so it can be created automatically at the end of another process. For example if you have a custom form using the webform module and would like the output of that form to trigger an action that created a node, how would you work around having to manually find the form_build_id? Maybe I’m over complicating this issue. What I’m trying to do is allow users to create nodes but limit the fields they can actually access.
Nice explanation but it does NOT work in the current Drupal version with php 5.3.
Probably should the book modul implement hook_forms.
works for me
Good starter post.
How do I define a path for my new node? Or more specific how do i stop path auto from kicking in by default ? My non-working code looks like:
$form_state['values']['path'] = array(
‘alias’ => $data['product_id'] . ‘/’ . $data['title'],
‘pathauto’ => 0
);