Drupal solutions for news sites: Creation date vs. publish date

9 Jun

This is part 3 in a series of posts about Drupal solutions for common issues raised in developing news sites. See a brief preamble in my first entry about the topic.

Solution 3: “This article says it was published way earlier than it was actually published.”

This is a seemingly small problem, and a quick fix. But it's subtle, and a good illustration and how sometimes the programmers and journalists have different ways of thinking about the content.
The first time someone brought this to my attention, I was confused. It sounded like a bug report. Perhaps I was using some kind of bad date stamp format in the date() function or something. The conversation went something like this:

Me– “So the article says it was created several hours earlier than you created it?”
Producer– “Yeah. I just published it now, actually, but it says it was published yesterday afternoon.”
Me– “Wait, published or created?”
Producer– “Published, I guess? What's the difference?”

As you might be able to guess, what I was referring to was the time at which the node had been first inserted into the database. In my mind, as a programmer, this is the true “creation” date of the article. To the editorial people, this is not meaningful. They routinely create articles in what we internally call “draft mode” (i.e., with “published” unchecked in the node form). By their logic, the date displayed on an article should be the moment at which it first become available for public consumption — that is, the time at which the node was edited in such a way that “published” went from false to true. When the article was first inserted (as a draft) doesn't matter at all to them.

To do this requires a trivial (but clever, in my opinion) fix. You will need to create a new module, so see this tutorial if you're not familiar with the process. Your module can consist entirely of the following code (let's name the module “article_form”):

 * Implementation of hook_form_alter.
function article_form_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'article_node_form') {
    if ($form['#node']->nid && !$form['#node']->status) {

This code will simply blank out the creation date field on a node when it's being edited and is currently marked as unpublished. If this field is blank on node submission, it will be automatically set to the present time. This means that each edit of the node when it's unpublished will bump the date up to the current time, until the edit that publishes it, which is the one that counts. Once it's published, that existing date will remain the default for further edits. As usual, change “article_node_form” to “story_node_form” or “danceparty_node_form” or whatever the name of your node type is.

The only caveat here is that if the article is unpublished at any point, the date will blank out again. Probably not that big of a deal since that's not something you'd be likely to do unless the article needed some serious reworking, so you could manually reset the date if it were that big of a deal.