Display a node's "last edited by" information.

Last modified: January 5, 2008 - 17:41

Let's say that you are the type of Drupal website where many different users change your nodes around, and you want to display the most recent author and date of that node's most recent revision.

I have done this by editing my node.tpl.php file in my theme's directory as thus:

<?php
   
   
//Display a more Wiki like information blurb
    //about who last edited and when for this node.
   
   
$nodeid = $node->nid;
    if (isset(
$nodeid)) {
   
$result = db_query("SELECT pv.value AS last_editor,
                        u.uid AS the_uid
            FROM node_revisions nr, users u, profile_values pv
                            WHERE    nr.uid = u.uid
                            AND    pv.fid = 1
                            AND    nr.uid = pv.uid
                            AND    nr.nid = "
.$nodeid. "
                            ORDER BY timestamp DESC
                            LIMIT 1"
);
   
$resultset = db_fetch_object($result);
    print
'Last edited by <a href="user/' .$resultset->the_uid. '">'
   
.$resultset->last_editor.'</a> on '
       
.format_date($changed);
    }
?>

In my Drupal installation, we have a custom profile value for "Full Name". In the database, this profile_value has an fid = 1, hence the SQL statement joining on this information.

If you do not have this profile value for full names, you can simply display the username. The code would look like this:

<?php
   
   
//Display a more Wiki like information blurb
    //about who last edited and when for this node.
   
   
$nodeid = $node->nid;
    if (isset(
$nodeid)) {
   
$result = db_query("SELECT u.name AS last_editor, u.uid AS the_uid
                          FROM node_revisions nr, users u
                            WHERE    nr.uid = u.uid
                            AND    nr.nid = "
.$nodeid. "
                            ORDER BY timestamp DESC
                            LIMIT 1"
);
   
$resultset = db_fetch_object($result);
    print
'Last edited by <a href="user/' .$resultset->the_uid. '">'
           
.$resultset->last_editor.'</a> on '.format_date($changed);
    }
?>

Thanks for your snippet! I

yaph - October 3, 2006 - 16:14

Thanks for your snippet!

I adopted your code to display the original author and the last edited information. I also modified the link to the author’s profile, so it is only displayed if the current user actually has access to user profiles.

I put this new function in my template.php file:

<?php
function mytheme_last_edit_info($nid) {
  if (isset(
$nid)) {
   
$result = db_query('SELECT n.changed, u.name, u.uid
                        FROM {node} n, {users} u
                        WHERE n.status=1 AND n.uid = u.uid
                        AND n.nid = %d'
, $nid );
    if (
$nodeinfo = db_fetch_object($result)) {
     
$name = $nodeinfo->name;
      if (
user_access('access user profiles')) {
       
$author = l($name, 'user/'. $nodeinfo->uid,
        array(
'title' => t('View user profile.')));
      } else {
       
$author = check_plain($name);
      }
     
$output = '<div class="info">'.
       
t('Submitted by %a. Last edited on %b.',   
        array(
'%a' => $author, '%b' =>
       
format_date($nodeinfo->changed))) .'</div>';
      return
$output;
    }
  }
}
?>

and these lines in my node.tpl.php file:

<?php if ($node->type == 'book') {
  print
mytheme_last_edit_info($node->nid);
}
?>

So the info is only displayed for book pages, because these are more likely to be updated than blog entries, at least in my case.

--
Ramiro.org | Torlaune.de

I'm using

markusl - March 30, 2007 - 09:59

I'm using this.

template.php:

<?php
function mytheme_last_edit_info($node) {
   
$result = db_query("SELECT u.name,
                        u.uid AS the_uid
                        FROM {node_revisions} nr, {users} u
                            WHERE    nr.uid = u.uid
                            AND    nr.nid = %d
                            ORDER BY timestamp DESC
                            LIMIT 1"
, $node->nid);
   
$resultset = db_fetch_object($result);

    if(
user_access('access user profiles')) {
       
$author = l($resultset->name, 'user/'. $resultset->the_uid,
                       array(
'title' => t('View user profile.')));
    } else {
       
$author = check_plain($resultset->name);
    }
      
    return
$author ." " .format_date($node->changed);
}
?>

node.tpl.php:

<?php
   
if($node->type == "page") {
        print
mytheme_last_edit_info($node);
    }
?>

Great snippet... using in Advanced Forum for topic posts

aharown07 - November 7, 2008 - 17:37

For Advanced Forum, you put the code in advf-forum-post.tpl.php.

(I'm using this version)

<?php
  
   
//Display a more Wiki like information blurb
    //about who last edited and when for this node.
  
   
$nodeid = $node->nid;
    if (isset(
$nodeid)) {
   
$result = db_query("SELECT u.name AS last_editor, u.uid AS the_uid
                          FROM node_revisions nr, users u
                            WHERE    nr.uid = u.uid
                            AND    nr.nid = "
.$nodeid. "
                            ORDER BY timestamp DESC
                            LIMIT 1"
);
   
$resultset = db_fetch_object($result);
    print
'Last edited by <a href="user/' .$resultset->the_uid. '">'
           
.$resultset->last_editor.'</a> on '.format_date($changed);
    }
?>

To display it where it makes sense (topic post only), you have to add the condition

<?php
if ($top_post):
?>
at the beginning (and required endif afterwards.... and omit the php start and end tags where appropriate).

Not sure I did all this correctly, but I also added a div= and named a class... and added css to advanced_forum.css. It works.

End result: nice little line (I put mine below the teaser links for now... not sure I like it there) reporting when the topic post/thread was last edited and by whom.

 
 

Drupal is a registered trademark of Dries Buytaert.