Programmatically modify


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // http://drupal.org/node/178506#comment-1056282
  2. // http://drupal.org/node/293663#comment-1056271
  3. // http://drupal.org/node/293663#comment-1056271
  4.  
  5. set_time_limit(0);
  6. $results = db_query("SELECT * FROM node WHERE type = 'post'");
  7. while ($post = db_fetch_object($results)) {
  8.  
  9. // Load existing node
  10. $node = node_load($post->nid);
  11. // Copy the existing Body field to the new CCK "field_body" value.
  12. $new_body = $node->field_body[0]['value'];
  13. // Merge in field_summary if it exists.
  14. if ($node->field_summary[0]['value']) {
  15. $new_body = $node->field_summary[0]['value'] . '<!--break-->' . $new_body;
  16. }
  17. // Keep same title.
  18. $new_title = $node->title;
  19.  
  20. // New handling for Drupal 6
  21. $form_state = array();
  22. module_load_include('inc', 'node', 'node.pages'); // new for Drupal 6
  23.  
  24. // If weren't loading existing nodes, but instead wanted to create new nodes,
  25. // we would initialize it like so:
  26. // $node = array('type' => 'post'); // a variable holding the content type
  27.  
  28. // $form_state['values']['type'] = 'post'; // the type of the node to be created
  29. // $form_state['values']['status'] = 1; // set the node's status to Published, or set to 0 for unpublished
  30. $form_state['values']['title'] = $new_title; // the node's title
  31. $form_state['values']['body'] = $new_body; // the body, not required
  32.  
  33. // The username of the node's author, must be a valid user with access to create this node.
  34. // 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
  35. $form_state['values']['name'] = 'Developer';
  36. $form_state['values']['op'] = t('Save'); // this seems to be a required value
  37.  
  38. // CCK field examples for CCK in D6
  39. // $form_state['values']['field_mydropdown']['value'] = 3; // a select (dropdown, where 3 is the "option" value saved to the db
  40. // $form_state['values']['field_myuserref'][0]['uid']['uid'] = 'someusername'; // a user reference example
  41. // $form_state['values']['field_mycheckbox']['value'] = 1; // a checkbox example
  42. // $form_state['values']['field_mynoderef'][0]['nid']['nid'] = '[nid:9]'; // a node reference example. In this case, 9 is referring to node 9
  43.  
  44. // Call the function to create the node, node_revisions, and CCK values.
  45. // Replace "story" with the name of your form / content type
  46. $errs = drupal_execute('post_node_form', $form_state, (object) $node);
  47.  
  48. // if there were any validation errors, drupal_execute will return them, then you have a chance to do something
  49. if (count($errs)) {
  50. // optional code here
  51. }
  52.  
  53. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.