Module Development - Manually Creating Node Revisions

If you've ever done any Drupal module development, then you've probably run across the node_save() function. This little beauty is the equivalent of the "submit" button on a node form. If you know anything about module development, you know that any module can tap into the "node save" process via hook_nodeapi(). By manually creating a node in code an saving it to the database with node_save(), you allow all the other modules on your site the opportunity to add their magic to your new node.

One important thing to note, however, is that when you create your node programatically, you don't always automatically get the default settings for your node as set up in your content type definition. I saw this recently when trying to update a node programatically (again, using node_save()). Since I had set up the content type with the "Create new revision" box checked, I assumed that each time I called node_save() from my custom module, I'd get a new revision. Not so. In order to get the new revision to save, I had to add

$node->revision = 1;

right before the call to node_save().

So, the lesson here is that even though some things are set as "default" in the content type definition, sometimes you'll have to set them explicitly when doing things programatically.

Sign up to receive email notifications of whenever we publish a new blog post or quicktip!