Return to Snippet

Revision: 22117
at January 2, 2010 23:35 by zachharkey


Initial Code
// http://drupal.org/node/178506#comment-1056282
// http://drupal.org/node/293663#comment-1056271
// http://drupal.org/node/293663#comment-1056271
  
set_time_limit(0);
$results = db_query("SELECT * FROM node WHERE type = 'post'");
while ($post = db_fetch_object($results)) {

  // Load existing node
  $node = node_load($post->nid);
  // Copy the existing Body field to the new CCK "field_body" value.
  $new_body = $node->field_body[0]['value'];
  // Merge in field_summary if it exists.
  if ($node->field_summary[0]['value']) {
    $new_body = $node->field_summary[0]['value'] . '<!--break-->' . $new_body;
  }
  // Keep same title.
  $new_title = $node->title;
  
  // New handling for Drupal 6
  $form_state = array();
  module_load_include('inc', 'node', 'node.pages');  // new for Drupal 6

  // If weren't loading existing nodes, but instead wanted to create new nodes,
  // we would initialize it like so:
  // $node = array('type' => 'post'); // a variable holding the content type

  // $form_state['values']['type'] = 'post'; // the type of the node to be created
  // $form_state['values']['status'] = 1; // set the node's status to Published, or set to 0 for unpublished
  $form_state['values']['title'] = $new_title;   // the node's title
  $form_state['values']['body'] = $new_body; // the body, not required

    // The username of the node's author, must be a valid user with access to create this node.
    // Otherwise, see (http://drupal.org/node/178506#comment-726479) above about using user_load() to create nodes as any user, even when not logged in
  $form_state['values']['name'] = 'Developer'; 
  $form_state['values']['op'] = t('Save');  // this seems to be a required value

  // CCK field examples for CCK in D6
  // $form_state['values']['field_mydropdown']['value'] = 3;    // a select (dropdown, where 3 is the "option" value saved to the db
  // $form_state['values']['field_myuserref'][0]['uid']['uid'] = 'someusername';  // a user reference example
  // $form_state['values']['field_mycheckbox']['value'] = 1;  // a checkbox example
  // $form_state['values']['field_mynoderef'][0]['nid']['nid'] = '[nid:9]';  // a node reference example. In this case, 9 is referring to node 9

    // Call the function to create the node, node_revisions, and CCK values.
    // Replace "story" with the name of your form / content type
  $errs = drupal_execute('post_node_form', $form_state, (object) $node);

    // if there were any validation errors, drupal_execute will return them, then you have a chance to do something
  if (count($errs)) {
    // optional code here
  } 

}

Initial URL

                                

Initial Description

                                

Initial Title
Programmatically modify

Initial Tags
textmate

Initial Language
Other