Drupal: add revision data to node

Drupal

Drupal can display the author of a node and the time it was created. Sometimes, it is handy to be able to display the author and time stamp of the last change (for auditing purposes).

Here is how you can add a node template variable that shows precisely that:

Submitted: June 6, 2009 - 10:26 — jack
Revised: August 26, 2009 - 09:13 — jill

We will store the revision data in a variable that we populate in template.php. First I'll show the code, then I'll explain.

The code is stored in the phptemplate_preprocess_node() function. If the function does not exist, just create it. If it already exists, add it to the existing function but make sure the parameter $vars matches with what you have (some use $vars, some prefer $variables).

<?php
function phptemplate_preprocess_node(&$vars) {

 
// --- Create revision data
  // Check if node was revised
 
if ($vars['created'] != $vars['changed']) {
   
// Check if the revision author was also
    // the node author
   
if ($vars['uid'] == $vars['revision_uid']) {
     
// Here: node author=revision author,
      // copy name from node
     
$rev_name = $vars['name'];
    } else {
     
// Here: revision author is different,
      // load user
     
$user = user_load(array('uid' => $vars['revision_uid']));
     
$rev_name = theme('username', $user);
    }
   
// Turn everything into a variable
    // for the node template
   
$vars['revision'] =
     
t('Revised: !datetime — !username',
        array(
         
'!datetime' => format_date($vars['changed']),
         
'!username' => $rev_name
       
)
      );
  }

}
?>

First of all, let's go over the variables accessible from the node template:

$created

Time stamp the node was created.

$changed

Time stamp the node was last revised. If the node does not yet have any revisions both $created and $changed will be the same.

$uid

User ID of the node author.

$revision_uid

User ID of the revision author.

$name

User name of the node author. I guess Drupal stores the full name in case the user is ever removed from the system.

With <?php if ($vars['created'] != $vars['changed']) ?> we check to see if the node was actually revised, because if it isn't, we can simply return an empty string.

Next, we check to see if the author of the revision was different from the author of the node: <?php if ($vars['uid'] == $vars['revision_uid']) ?>. If the authors are the same, we use the name available in the node template. If the authors are different, we need to retrieve it with user_load().

In the node.tpl.php template we will add the following:

<?php if ($submitted) { ?>
  <div class="submitted">
    <?php print $submitted; ?>
    <div>
      <?php print $revision; ?>
    </div>
  </div>
<?php } ?>

By placing the revision data inside the existing <div class="submitted"> for the submitted data it will inherit the styles. Just ensure you change the class name to whatever you are using (I just used the class name from Garland as an example). By placing it in a separate <div> we will ensure it is displayed on a separate line.

If you only want to display the last revised date for those with access to the revisions, you can modify the first comparison in template.php to include a check:

<?php
//modify phptemplate_preprocess_node()
if ($vars['created'] != $vars['changed']
  &&
user_access('view revisions')) {

 
//...etc

}
?>

If you want to display the log message that was entered, you could include:

<?php
//modify phptemplate_preprocess_node()
$vars['revision'] .= (strlen($vars['log'])>1) ? $vars['log'] : t('Revised');
?>

This will display the log message or the text "Revised" if no message was entered.

By the way, you can change the display of the submitted data per content type in your theme settings admin/build/themes/settings.

Drupal 7

This code also works for Drupal 7. However, the loading of the user account is slightly different:

<?php
function phptemplate_preprocess_node(&$vars) {

 
// --- Create revision data
  // Check if node was revised
 
if ($vars['created'] != $vars['changed']) {
   
// Check if the revision author was also
    // the node author
   
if ($vars['uid'] == $vars['revision_uid']) {
     
// Here: node author=revision author,
      // copy name from node
     
$rev_name = $vars['name'];
    } else {
     
// Here: revision author is different,
      // load user (D7)
     
$user = array('account' => user_load($vars['revision_uid']));
     
$rev_name = theme('username', $user);
    }
   
// Turn everything into a variable
    // for the node template
   
$vars['revision'] =
     
t('Revised: !datetime — !username',
        array(
         
'!datetime' => format_date($vars['changed']),
         
'!username' => $rev_name
       
)
      );
  }

}
?>

Comments

hey thank you very much for that! do you have any idea how i could use this function in drupal 7?

best regards
paul

You should be able to use the same code in your Drupal 7 template. I just tested it in the default Bartik theme using the function bartik_preprocess_node(&$variables) { }. I just replaced the $vars with $variables.

Hello,
Great tutorial, I searched for this.
But it's new for me, i have a subtheme and I add this in my subtheme.
I replace 'phptemplate_preprocess_node()' in 'subtheme_preprocess_node()' is this right wat i'm say.? Can you help me?
It's for this site: www..............com. The events are created by an author but can be updated by another author and I want display the dates and names created and updated for every event. Now it's display only the created information

Correct, you can use that, as long as subtheme represents your actual theme name.

(I obfuscated the URL.)

Hey i guess there is an error in the HTML somewhere? I can't load the page properly !