How to programmatically create nodes, comments and taxonomies in Drupal 7

I've been busy with a migration work for my client recently. There is an established website built with some proprietary CMS with a decent amount of articles / pages, different categories and user comments. My team was hired to make a complete migration from this money-sucking old-fashioned proprietary CMS to a modern shiny open-source Drupal 7. I like this kind of tasks. It’s always a pleasure to help people get rid of an old and proprietary technologies.

To accomplish this task, I needed to create nodes, comments and taxonomies programmatically in Drupal 7, which is a pretty trivial task for Drupal 6. But in D7 with the introduction of entities and fields in core (aka CCK in core) things have changed a bit. In today's post I'll show you how to programmatically create nodes, comments and taxonomies in Drupal 7.

In detail you’ll find out how to:

1. Programmatically create a node:

  • Initialize a node object
  • Add body field
  • Add custom fields
  • Add file / image field
  • Add a term to a node
  • Save a node

2. Programmatically create a comment

3. Programmatically create a term

1. How to programmatically create a node

1.1 Initialize a node object

1
2
3
4
5
6
7
  1. $node = new stdClass(); // We create a new node object
  2. $node->type = "page"; // Or any other content type you want
  3. $node->title = "Your title goes jere";
  4. $node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
  5. $node->path = array('alias' => 'your node path'); // Setting a node path
  6. node_object_prepare($node); // Set some default values.
  7. $node->uid = 1; // Or any id you wish

*We set LANGUAGE_NONE for $node->language, if you don’t have the locale module enabled, the node will not be assigned any particular language. So that's why we put here the constant LANGUAGE_NONE. In Drupal, nodes and fields can exist in more that one language, so if your site is multilingual you should specify the language code for your field. You can configure languages and get language codes by going this path in Drupal administration: Configuration -> Regional and language -> Languages.

1.2 Add a body field

1
2
3
4
  1. // Let's add standard body field
  2. $node->body[$node->language][0]['value'] = 'This is a body text';
  3. $node->body[$node->language][0]['summary'] = 'Here goes a summary';
  4. $node->body[$node->language][0]['format'] = 'filtered_html'; // If field has a format, you need to define it. Here we define a default filtered_html format for a body field

1.3 Add custom fields

1
2
3
4
5
  1. // Let's add some CCK/Fields API field. This is pretty similar to the body example
  2. $node->field_custom_name[$node->language][0]['value'] = 'This is a custom field value';
  3. // If your custom field has a format, don't forget to define it here
  4. $node->field_custom_name[$node->language][0]['format'] = 'This is a custom field value';
  5. // And etc. you can add as much fields here as your content type has. The sky is the limit... and the server specs, of course ;)

1.4 Add file / image fields

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  1. // Some file on our system
  2. $file_path = drupal_realpath('somefile.png'); // Create a File object
  3. $file = (object) array(
  4. 'uid' => 1,
  5. 'uri' => $file_path,
  6. 'filemime' => file_get_mimetype($file_path),
  7. 'status' => 1,
  8. );
  9. $file = file_copy($file, 'public://'); // Save the file to the root of the files directory. You can specify a subdirectory, for example, 'public://images'
  10. $node->field_image[LANGUAGE_NONE][0] = (array)$file; //associate the file object with the image field:

1.5 Add a term to a node

1
  1. $node->field_tags[$node->language][]['tid'] = 1;

field_tags here is the name of a term reference field attached to your content type, 1 is a term id you wish to assign to a node. Simple!

1.6 Save a node

1
2
  1. $node = node_submit($node); // Prepare node for a submit
  2. node_save($node); // After this call we'll get a nid

2. How to programmatically create a comment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  1. // Let's create a managed object $comment = new stdClass(); // We create a new comment object
  2. $comment->nid = $node->nid; // nid of a node you want to attach a comment to
  3. $comment->cid = 0; // leave it as is
  4. $comment->pid = 0; // parent comment id, 0 if none
  5. $comment->uid = 1; // user's id, who left the comment
  6. $comment->mail = 'email@example.com'; // user's email
  7. $comment->name = 'User name'; // If user is authenticated you can omit this field, it will be auto-populated, if the user is anonymous and you want to name him somehow, input his name here
  8. $comment->thread = '01/'; // OPTIONAL. If you need comments to be threaded you can fill this value. Otherwise omit it.
  9. $comment->hostname = '127.0.01' // OPTIONAL. You can log poster's ip here
  10. $comment->created = time(); // OPTIONAL. You can set any time you want here. Useful for backdated comments creation.
  11. $comment->is_anonymous = 0; // leave it as is
  12. $comment->homepage = ''; // you can add homepage URL here
  13. $comment->status = COMMENT_PUBLISHED; // We auto-publish this comment
  14. $comment->language = LANGUAGE_NONE; // The same as for a node
  15. $comment->subject = 'Comment subject';
  16. $comment->comment_body[$comment->language][0]['value'] = 'Comment body text'; // Everything here is pretty much like with a node
  17. $comment->comment_body[$comment->language][0]['format'] = 'filtered_html';
  18. $comment->field_custom_field_name[LANGUAGE_NONE][0]['value'] = ‘Some value’; // OPTIONAL. If your comment has a custom field attached it can added as simple as this // preparing a comment for a save
  19. comment_submit($comment); // saving a comment
  20. comment_save($comment);

3. How to programmatically create a taxonomy term

This one is the most easiest part of the tutorial. To create a term you just need to do the following:

1
2
3
4
5
  1. $term = new stdClass();
  2. $term->name = ‘Term Name’;
  3. $term->vid = 1; // ‘1’ is a vocabulary id you wish this term to assign to
  4. $term->field_custom_field_name[LANGUAGE_NONE][0]['value'] = ‘Some value’; // OPTIONAL. If your term has a custom field attached it can added as simple as this
  5. taxonomy_term_save($term); // Finally, save our term

Well, that’s all for today my friends, hope this article helps you. Please leave your comments and additions and if you liked the post, share it on twitter and facebook, spread the knowledge!