1

How do I add a footer to each page generated by Drupal (7) so to show the identity of the last user that modified that page? (together with the modification date)

2 Answers 2

0

I think you should be able to do this with something like this, note that this is only pseduo code and is not tested:

 // get the user that made the latest modification
 // for a node of type 'page'
 function getLatestPageUpdater() {
     $query = "SELECT n.changed, u.* FROM node n 
               INNER JOIN users u ON u.uid = n.uid
               WHERE n.type = 'page' 
               ORDER BY changed DESC 
               LIMIT 1";
     return db_query($query)->fetch();
 }

Then in your page.tpl.php you could do like this:

 <?php $user = getLatestPageUpdater();
       if($user):
 ?>
 <p>Last edited <?php print $user->changed; ?> by <?php print $user->name; ?></p>
 <?php endif; ?>

Like @tostinni pointed out Drupal doesn't track the last editor of a node, so the above code would always display the original author of the node. If you're using node revisions I think you should be able to do something like this (again this is only pseudo code and is not tested):

function getLatestPageUpdater($node) {
  $query = "SELECT u.name
            FROM node_revisions nr, users u
            WHERE nr.uid = u.uid
            AND nr.nid = ".$node->nid."
            ORDER BY timestamp DESC
            LIMIT 1";
  return db_query($query)->fetch();
}

<?php if($node->type == "page"): ?>
<?php $user = getLatestPageUpdater($node); 
      if($user): 
?>
<p>Last edited <?php print $node->changed; ?> by <?php print $user->name; ?></p>
<?php endif; ?>
<?php endif; ?>
6
  • That is exactly what I was looking for, thank you!
    – Umberto
    Jun 15, 2012 at 13:55
  • @Umberto - Nice it solved your problem. Jun 15, 2012 at 22:22
  • 1
    @Umberto I know this is quite late. page.tpl.php already defines a $node variable that contains information about the currently loaded node, including exactly the information you're looking for. There is no need to perform an extra DB query in your template to fetch it.
    – 9ee1
    Feb 13, 2013 at 2:25
  • 1
    @KristerAndersson I'm late on this one but don't forget that Drupal doesn't track the last username that changed the node, so <?php print $user->name; ?> will always display the author of the node.
    – tostinni
    Oct 24, 2013 at 11:18
  • @tostinni - You're completely right. Didn't knew that Drupal wasn't tracking the user who changed a post. I guess the above code could be modified to grab the last editor if one were to use revisions. Oct 28, 2013 at 18:46
0

I've written a Last Modification Date module for drupal that does exactly that.

ATM this is still a sandbox project, but it is in approval state.

It just creates a block that spits out the last modification date of any node in the system. There is no database polling, saving of that date is done at node save time. So no overhead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.