Publishing Draft Nodes in Drupal 7

This article explains how to set the creation date of a draft article published in Drupal 7 to the current date, rather than the date the draft was originally created.

Existing Solutions

Naturally, what I was trying to solve wasn’t an entirely new problem. My need to update the date of a newly published node wasn’t complex so I immediately pursued my own solution. After reviewing the existing solutions, I decided to share my own in case it proves more useful. First, I would like to discuss two existing project from Drupal.org called Publication Date and Publish Date.

Publication Date provides a fairly nice and thorough solution. An additional field is presented for the publication date, which can default to the current date. Field access can be restricted through the permissions.

Publish Date provides a much simpler solution, aside from its support for the Wordbench Moderation module. Unfortunately, there are flaws with the solution. The module uses a form_alter() function to unset the authoring date of the node_edit form. This is possible because the install file sets the module weight to 9999.

A Different Solution

I developed my own solution, which I added directly to a custom module. I always create a custom module for every Drupal project. If you don’t already have a custom module, you can follow these instructions to create one. I used the hook_node_presave() function.

//presave function
function custommodule_node_presave($node) {
	//node has been newly published, update creation date
	if($node->status == 1 && $node->original->status == 0) {
	  $node->created = $node->changed;
	}
}

Here you would replace custommodule with your own module name. If your were to print_r() or var_dump() the $node variable, you would see that the original is also available. You can modify this approach to check for other types of changes in content and make changes before saving the node. The main drawback of this approach should be obvious: if a node has been published, unpublished and then published again, the date will be updated again.

Taking It Further

In my case, I only wanted to perform this operation on nodes of the blog content type.

//presave function
function custommodule_node_presave($node) {
	//node has been newly published, update creation date
	if($node->type == 'blog' && $node->status == 1 && $node->original->status == 0) {
	  $node->created = $node->changed;
	}
}

Photo of the The Astronomical Clock in Prague by Mark Knowles.