Creating nodes with CCK-fields programmatically using drupal_execute() in D6

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


43 points

I found that creating nodes with CCK-fields programmatically in D6 is somewhat a pain in the ass. I've had a problem with this issue for several weeks and I've done alot of debugging and
looking for documentation to find a good way of handling this. However, most documentation was incomplete, outdated, only compatible with D5 or so well hidden in other posts that it
was overlooked easily. That's why I decided to post my findings and personal way of working here, in the hope that it will save some other people, which come across the same issue,
alot of time.

There are alot of discussions out there regarding the use of node_save() Vs. drupal_execute(). I don't want to go much deeper into that matter right now. I myself prefer drupal_execute(),
despite it's complexity.

The following code presents a couple examples of various field-types. This is the minimum required for this method to work. There's probably a bunch of other stuff that you might want to
define, but I'm just focusing on the CCK-fields here.

<?php
function addSomeStuff() {
    module_load_include('inc', 'node', 'node.pages'); // required
    $node = array('type' => 'mytype');
    $form_state = array();
    $form_state['values']['title'] = 'someText'; // node's title
 
    $form_state['values']['field_user']['uid']['uid'] = someUid; // user reference. (Example: 1)
    $form_state['values']['field_node']['nid']['nid'] = someNid; // node reference. (Example: 143)
    $form_state['values']['field_text'][0]['value'] = 'someText'; // regular text field
    $form_state['values']['field_number'][0]['value'] = 10; // regular integer field
    $form_state['values']['field_checkbox']['value'] = 1; // on/off checkbox field
 
    $form_state['values']['op'] = t('Save'); // required value
    drupal_execute('mytype_node_form', $form_state, (object)$node);
 
    // you can probably configure the node-author in $form_state or $node,
    // but i'm doing it this way to demonstrate the use of $form_state['nid'].
    // the following lines aren't required, but otherwise the node-author will be "Anonymous" in this case.
    $node = node_load($form_state['nid']); // nid from the node that gets created is set in $form_state['nid'] after drupal_execute()
    $node->uid = 1; // set author to admin
    node_save($node);
}

Other useful links: http://drupal.org/node/178506

Kind regards,

mertens



-2 points

This will time out if you try using it on multiple saves at once, I prefer node_save as you can implement it into the batch_api.

Pobster

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.