Community Documentation

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

Last updated January 5, 2008. Created by LeeHunter on August 29, 2006.
Edited by sepeck, mikeatlas. Log in to edit this page.

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);
    }
?>

Comments

Thanks for your snippet! I

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

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

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 reporting when the topic post/thread was last edited and by whom.

This works for me in

This works for me in D6.17

<?php
   
<?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
                                        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
"Zuletzt bearbeitet von " . l($resultset->last_editor, 'users/' . $resultset->last_editor) . " am " .format_date($changed);
    }
?>

?>

Read

Read http://drupal.org/node/2497 on why you should use placeholders:

$result = db_query("SELECT u.name AS last_editor FROM node_revisions nr, users u
    WHERE    nr.uid = u.uid
    AND    nr.nid = %d
    ORDER BY timestamp DESC
    LIMIT 1", $nodeid);

How to add "Last Editor" to a View?

I would like to add the "Last Editor" to a View.

The view would be a table and show
Title - Last Edited (time) - Last Edited by (Last Editor)

What would be the php code to add to Views in Custom Field php-code??

Thanks for all pointers ....

-----------
Good luck .....
... more recent results of trying Drupal just once are -
www.native-power.de
Malls and More

resolved

-----------
Good luck .....
... more recent results of trying Drupal just once are -
www.native-power.de
Malls and More

Correct link to the solution

I am also looking for the solution for this problem.

Thanks @tryitonce. But the correct link to the solution should be here http://drupal.org/node/912872

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here