17 March 2011

I needed to get some xml data and generate nodes out of them. This shows how I created nodes programmatically from my module in Drupal 7. I also suggest looking into creating a node using entity metadata wrapper.

Source code viewer
  1. // Get current user for id.
  2. global $user;
  3.  
  4. // Make new object for an object.
  5. $node = new stdClass();
  6. // Title for the node.
  7. $node->title = 'title for node';
  8. // Node type - page exists in default.
  9. $node->type = 'page';
  10. // User id - who owns/created the node
  11. $node->uid = $user->uid;
  12. // Set the language of the node to neutral.
  13. $node->language = LANGUAGE_NONE;
  14. // Node body - default field.
  15. $node->body[LANGUAGE_NONE][0]['value'] = 'node body';
  16.  
  17. // Custom field - made by fields/cck.
  18. $node->field_in_store[LANGUAGE_NONE][0]['value'] = '';
  19. // Taxonomy term number.
  20. $node->field_category[LANGUAGE_NONE][0]['tid'] = 1;
  21.  
  22. // Save / create the node.
  23. node_save($node);
Programming Language: PHP