Login

Drupal: add revision data to node

stplanken — August 30, 2009 - 07:01

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

}
?>

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

Tags: Drupal, Drupal6

Post new comment

Image CAPTCHA
Enter the characters shown in the image.