Syndicate

Feed

Adding 'Last edited by name some time ago' information

Administrators are presented with a special marker when content is new or has been updated in the Administration section of their site at webSite.com/admin/content/node. Additionally, in node lists, the module tracker informs any logged-in user if he or she hasn't read a particular (recently created) node (using the marker new), or if a node he/she's read already was modified (using the marker updated). You, the themer, may want everyone, including “anonymous users”, to be informed of updates to nodes right inside their content. This information may specify who last edited the node and when. Note that the last editor of a content may not be the creator of that content, and I'll take this into account in my “solution”. In the following theming tweak, you'll add Last edited by name some time ago information to your nodes' content.

Here is what you'll try to achieve:

Last edited by name some time ago.

Solution

  1. Edit the file template.php to pass an additional variable to the node template, a variable we'll call $last_edit...

  2. print that variable in our node.tpl.php file, and...

  3. style this added markup as needed in style.css.

Before you apply this solution to your theme, we will review how Drupal deals with node editing and node revisions. Read carefully.

When a node is simply edited, with no new revision created

If the editor of the node doesn't edit the Authored by field under Authoring information, the recorded author of the node remains unchanged. What that means is that the variables $name and $uid in node.tpl.php will output the same HTML. However, if the editor is not the person who authored the node, the revision_uid (user ID) of the current revision does change to show who edited the node. In node.tpl.php, the variable $revision_uid always tells us who was the last person to edit the current version of a node.

If the editor changes the Authored by field under Authoring information, the variables $name and $uid in node.tpl.php will change to reflect whatever user the editor has picked as new author. However, still in node.tpl.php, the variable $revision_uid will provide us with the user ID of the editor.

No one with permission to edit a node can effectively edit it without a record of his action being saved to the Drupal database, a record of his action under his name. The info about any node version in the table {node_revision} will tell us when and by whom that version was last edited.

When a new revision of a node is created

What has been said previously still applies. If the Authored by field isn't edited, the author of the new revision and the author of the old revision will be the same person, regardless of who created the new revision. The variable $revision_uid will provide us with the user ID of the creator of the new revision.

If the editor changes the Authored by field under Authoring information when creating a new revision, the info about the author of the node will reflect that change.

Variables available to node.tpl.php — that you will use

What follows is a partial list of the variables available to node.tpl.php.

Available variables:
* - $page: Flag for the full page state.
* - $name: Themed username of node author output from theme_username().
* - $uid: User ID of the node author.
* - $vid: Current version ID of the node, e.g. the version to output.
* - $revision_uid: User ID of the last editor of the node version with $vid.
* - $created: Time the node was published formatted in Unix timestamp.
* - $changed: Time the node was last edited formatted in Unix timestamp.

For a given revision row in the {node_revisions} table of your Drupal database, the variable $revision_uid corresponds to the uid column. Whereas the $uid variable is read from the uid column in the table {node}.

You can find an exhaustive list of the variables available to node.tpl.php if you inspect a node's content using the Themer Info widget that comes with the Devel module. Look under Template Variables:

Complete list of variables available in node.tpl.php

Passing a new variable to print in the node template

You will use a prepocess function to pass a new, e.g. additional, variable to your node template, ie: $last_edit. Hence, you will need to add a phptemplate_preprocess_node() function if it has not already been defined to your template.php file. Open your theme template.php file in a text editor, and add the following code (please do read the comments):

/**
* Override or insert PHPTemplate variables into the node template.
*/
function phptemplate_preprocess_node(&$vars) {
  /*
   * If the node is shown on its dedicated page and
   * the node was edited after its creation. 
   */
  if ($vars['page'] && $vars['created'] != $vars['changed']) {
    $time_ago = format_interval(time() - $vars['changed'], 1);
    /* 
     * If the last person who edited the node 
     * is NOT the author who created the node.
     */
    if ($vars['uid'] != $vars['revision_uid']) {
      // Let's get the THEMED name of the last editor.
      $user = user_load(array('uid' => $vars['revision_uid']));
      $edited_by = theme('username', $user);
    }
    /* 
     * If the last person who edited the node 
     * is also the author who created the node,
     * we already have the THEMED name of that person.
     */
    else {
      $edited_by = $vars['name'];
    }
    /* Adding the variable. */
    $vars['last_edit'] = t('Last edited by !name about @time ago.', 
      array('!name' => $edited_by, '@time' => $time_ago));
  }
 
}

After that, you edit your node.tpl.php file to print your new variable. Take note that the variable will be undefined if we're on a node list page, or if the node wasn't edited since its creation. So we won't add a div unless our variable is defined.

<?php if ($last_edit): ?>  
  <div class="last-edit clear-block">
    <small><?php print $last_edit; ?></small>
  </div>
<?php endif; ?>

A bit of styling

You can pick a special color for that added text, and increase the white space above and below it, with this CSS rule, added to the style.css stylesheet of your theme:

/**
 * Last edit rules
 */
 
.last-edit {
  color: #ca481c;
  margin: 1em 0pt; 
}

Because it makes use of a preprocess function, this solution will work in Drupal 6 only. There's an equivalent method for Drupal 5 themes, and if someone asks for it I will provide it.

A few questions and their answer

Question 1. Why use the function theme('username', ...)? Why aren't we just outputing the name of the editor as is?

You could. When using the function theme('username', $user), the name of the user becomes a link to his profile page under certain conditions. It's very likely you may not like that. If you don't, just output the name like so (this is a snippet only):

/* 
 * If the last person who edited the node 
 * is NOT the author who created the node.
 */
if ($vars['uid'] != $vars['revision_uid']) {
  $user = user_load(array('uid' => $vars['revision_uid']));
}
/* 
 * If the last person who edited the node 
 * is also the author who created the node,
 * we already have the THEMED name of that person.
 */
else {
  $user = user_load(array('uid' => $vars['uid'])); 
}
 
/* Reading the name property of the user object */
$edited_by = $user->name;
 
/* Adding the variable. */
$vars['last_edit'] = t('Last edited by @name about @time ago.', 
  array('@name' => $edited_by, '@time' => $time_ago));

Note that I am using @name instead of !name in the translation function now. That's because I want the user name to be escaped properly and sanitized before being output to screen. Always do this for user names. The ! prefix used for a placeholder means that the variable it stands for should be used as is.

Question 2. Are variables publicized as being available in a template are the same ones we can read in the parameter $vars passed to a preprocess function, right?

Riiight.

Question 3. Some of these variables we're using don't seem to be listed at the beginning of the file modules/node/node.tpl.php...

True. They are missing from the comment area, because they are less commonly used. That's why the Devel module is tremendously useful to themers in Drupal 6: as a themer, you can use the Themer info widget to get an exhaustive list of the available variables for any template.

Question 4. You say that the variable $last_edit is undefined if we're on a page with a list of nodes, or if the node wasn't edited after its creation. Why is that?

Because of this condition in the preprocess function:

if ($vars['page'] && $vars['created'] != $vars['changed']) {

We are defining the new variable only when we pass through this condition.

$vars['page'] becomes $page in the node template and it's a TRUE/FALSE flag. It is TRUE when the node is shown on its dedicated page at webSite.com/node/x, and FALSE otherwise. You could change this to say if we're not showing the node in teaser view, and in most cases this would have the same effect, but take note that it is possible to show a list of nodes in full view, with the module Views for example.

Changing $vars['page'] to !$vars['teaser'] — and changing the != sign to a 'greater than' sign — will give your script the same behavior in most situations:

if (!$vars['teaser'] && $vars['created'] < $vars['changed']) {
Last edited by Caroline Schnapp about 5 years ago.

Comments

How about in Drupal 5

I have a Drupal 5 web site and I'd like to show this line of text below the main text only for a certain content type, for example CONTENT_TYPE. How do I go about this? How do you add a var to node.tpl.php in Drupal 5 to print only in nodes belonging to a certain content type? Thanks in advance.

In Drupal 5

Open your template.php file for edit. Look for a function called function _phptemplate_variables($hook, $vars). It may already have been defined in your theme. If not create one. If you have such definition already, it probably uses a switch statement over $hook. In any case, when $hook is equal to 'node', that's when you will pass the new variable $last_edit. More so, you will only pass it if the node belongs to content type CONTENT_TYPE — and I assume if the other conditions I have listed are met. So... the code would be:

/**
 * Override or insert PHPTemplate variables into the templates.
 */
function _phptemplate_variables($hook, $vars) {
  switch($hook) {
    case 'node':
       /*
        * If the node is shown on its dedicated page and
        * the node was edited after its creation. 
        */
      if ($vars['page'] && $vars['type'] == 'CONTENT_TYPE'
               && $vars['created'] != $vars['changed']) {
        $time_ago = format_interval(time() - $vars['changed'], 1);
        $edited_by = $vars['name'];
        /* Adding the variable. */
        $vars['last_edit'] = t('Last edited by !name about @time ago.', 
           array('!name' => $edited_by, '@time' => $time_ago));
        return $vars;
      }
      break;
  }
  return array();      
}

Then you print $last_edit in your node.tpl.php file, below the node's content.

Note that reading the author of the last edit would require to query the database, something I avoid here. So this code will give credit to the node's author in all cases.

It works!

Thank you!

At first I had forgotten to replace the CONTENT_TYPE with the actual content type name. I understand that this 'type' variable has to be the machine-readable name of the content type. Anyone who reads this, know that.

I am not an expert to edit a

I am not an expert to edit a website or part of another website, I only know how to use it. In my opinion, a lot of things and basic techniques that must be mastered in order that we can edit and apply the necessary codes properly. Wood Puzzles

host-compared

A reliable informative post that you have shared and appreciate your work for sharing the information. hostcompared.com Got some entertaining information and would like to provide it a try. Applaud your work and keep sharing your information.

traveladvice

www.travelout.co For all those people who love to travel and explore the world, planning out the journey is important. Especially when it comes to travelling alone and that for the purpose of fun and not for some business affair, then definitely the budget required is more than usual.

That's not only a porn video

That's not only a porn video site, this is a new way of life for you.

Thank you for another

Thank you for another fantastic blog. Where else could I get this kind of information written in such an incite full way? I have a project that I am just now working on, and i am sure this will help me a lot..and I have been looking for such information since from few days.The info about any node version in the table {node_revision} will tell us when and by whom that version was last edited.
True Blood Seasons 1-6 DVD Box Set|Cold Case seasons 1-7 DVD Box set |Walt Disney's 100 Years Of Magic 164 Discs DVD Box Set

The resource that Stainless

The resource that Stainless steel raw material in Hyderabad | Canteen kitchen equipment in Andhra Pradesh | Ceiling fans exporters in India you mentioned here is something that I have been looking from quite a time. And finally it ended with such a nice blog post. Don’t have words to thank you.

I am not so adept to edit

I am not so adept to edit your blog look like. However, some steps and advice that you include on this page may be one thing I've learned in editing my blog to make it look more attractive and good. cheap homecoming dresses

http://professional--editing.com/

Thank you all for the information and the clear explanation. It is very easy to do it with these explanations. It works. Thanks once again.

Thank you all for the information and the clear explanation

Thank you all for the information and the clear explanation. It is very easy to do it with these explanations. It works. Thanks once again.

I genuinely enjoyed reading

I genuinely enjoyed reading it, you're a great author.The great information is here. I love such post bcz we can get a few useful information from this particular blog. I be expecting more post from you.
MCI Screening test after MBBS
MCI Screening test

Printing the current and updated in block

I have a slightly different need and been searching around the world to no luck. I hope you can lead me in the right direction.

I want to show a single line at the top of my header block, so it's not placed inside the content/node, that says:
Today date: blablabla | Updated: .... ago

The updated line will grab any possible new post created or simply changed throughout the whole site.

Any help would be appreciated. And thanks for all the light you've thrown.

Database query

You may do this 'simply' with a database query.

I am a little busy right now. Have you dropped your question in the Drupal.org forums? I am sure someone could help you write the query over there.

Been searching around drupal

Been searching around drupal for ages to no luck, not to my actual need. Finally got it through views. It's actually simpler than I thought. The only problem now is that all date variables like ago, hours, days, etc are not translatable and even can't be overriden with stringoverrides.module. Any thought? Or is it the problem with views 2 strings? Thanks, Caroline, for great tutorials.

or...

I tried your instructions but wasn't able to get it working.. so I did following

in node.tpl.php


<?php if ($submitted): ?>

<?php endif; ?>

was changed into


<?php if ($submitted): ?>

<?php endif; ?>

which seems to be working just great :)

If you want to hear a

If you want to hear a reader’s feedback :) , I rate this post for 4/5.

For Drupal 7 change in phptemplate_preprocess_node to:

*
* Extra variables for CoL metadatabase to show 'Last edited by name some time ago' information
*
* If the node is shown on its dedicated page and
* the node was edited after its creation.
*/
if ($variables['page'] && ($variables['created'] != $variables['changed'])) {
$time_ago = format_interval(time() - $variables['changed'], 1);
/*
* If the last person who edited the node
* is NOT the author who created the node.
*/
if ($variables['uid'] != $variables['revision_uid']) {
// Let's get the THEMED name of the last editor.
$user = user_load($variables['revision_uid']);
$uservars['account'] = $user;
$uservars['name'] = $user->name;
$uservars['link_path'] = user_uri($user);
$edited_by = theme('username', $uservars);
}
/*
* If the last person who edited the node
* is also the author who created the node,
* we already have the THEMED name of that person.
*/
else {
$edited_by = $variables['name'];
}
/* Adding the variable. */
$variables['last_edit'] = t('Last edited by !name about @time ago.',
array('!name' => $edited_by, '@time' => $time_ago));
}

Drupal 7 without user link

If users don't have access to user profiles, you should not show the link. In that case, delete the code $uservars['link_path'] = user_uri($user);

Worked like a charm!

There was certainly quite a few steps to go through there, but in the end it all worked great! I like that you can pick specific colors, it gives it soo much more character. Thanks for the great post and the great help! This blog is amazing!

Changing the look of a

Changing the look of a website design is a good thing. With the existence of such a change, we could be better in the blog application development plan. Although I do not know too much about design, but I like the look of the design are not excessive. health insurance Montana

Your knowledge of this

Your knowledge of this subject comes through clearly in this article. I love to read this kind of articles, I hope you will update it. Thank you for sharing it with me.
hotels in manali with tariff

I am writing a term paper on

I am writing a term paper on this topic and came across your post which was very helpful. Do you know where I can find more information about this?
injection machines

I am writing a term paper on

I am writing a term paper on this topic and came across your post which was very helpful. Do you know where I can find more information about this?
music management degree

Part of a class I am taking

Part of a class I am taking involves this particular subject and I am researching for information to use in an upcoming report. Your post is really helpful; do you have any others on this topic?
cheap furniture los angeles

re

The following pointers tips are perfect. It's my job to enjoy brainstorming almost difficult field lines. Sometimes it can be leather like, but we gift hit to do that which we staleness do - delight our readers!
winterurlaub Österreich |skiurlaub Österreich

Some genuinely interesting

Some genuinely interesting information, well written. Glad to be one of the visitors of your site.

Awesome thought provoking blogs

Awesome thought provoking blogs like yours will inspire conversation. The comments become another reason for people to stay and another reason for people to come back.Makrana Marble | Makrana Marble Price

Great news Post.

I am awaiting for a great news post on related this topic. And after more searching on internet at last I got some important information here. I really appreciate with you. Great news Post.
Resorts in Manali | 5 Star Resorts in Manali

right one

I will be directing my student to look at your post for good information. Speaking of feed pellets China

Admirable article stuff.

Our Intermediate school teachers always say some great people Questions every day in assembly for better understand to our life. Thanks for such a very admirable and remembered article stuff.

Nice One...

Excellent is the only word i can give u for this wonderful blog, keep it up. I will come back again to read some more interesting things on this topic. male enhancement

I've followed the advice in

I've followed the advice in this blog to edit my blog, and I managed to do it. In my opinion, the steps you mentioned in this blog is self-explanatory and easy to do.

Updating the blog is one way

Updating the blog is one way to attract visitors. Although not very often do this, but at least once a year I always do the update, and I could see an increase in visitors to do so. sunlighten sauna customer reviews

I am a novice in the field

I am a novice in the field of programming and computer like this. In my opinion, this is one of the interesting things to be learned because many of the codes should be combined with care. wheelchair lifts

This topic has always been

This topic has always been one of my favorite subjects to read about. I have found your post to be very rousing and full of good information. I will check your other articles shortly. help with writing a cv

Mitsubishi jet towel

Remarkable things here. I am very satisfied to look your Mitsubishi jet towel Thanks so much and I am having a visit to your

Drupal 7 Version without switch user links switch

For Drupal 7 you only have to edit one line of the original script above changing the theme function to accept an array instead of an object:

if ($vars['uid'] != $vars['revision_uid']) {
// Let's get the THEMED name of the last editor.
$user = user_load(array('uid' => $vars['revision_uid']));
- $edited_by = theme('username', $user);
+ $edited_by = theme('username', array('account' => $user));
}

And since I'm posting, here's a version that exposes three variables(changed_name, changed_picture and display_changed) as variables rather than last_edit for more theming options.

/*
*
* Extra variables to show changed_name, changed_picture and display_changed (boolean)
*
*/

/* Initializing variables */
$edit_name = 'Anonymous';
$edit_picture = '';
$edit_display = false;

if ($vars['created'] != $vars['changed']) {
$edit_display = true;
/*
* If the last person who edited the node
* is NOT the author who created the node.
*/
if ($vars['uid'] != $vars['revision_uid']) {
$user = user_load($vars['revision_uid']);
$edit_name = theme('username', array('account' => $user));
$edit_picture = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', array('account' => $user)) : '';
}
/*
* If editor also author who created the node,
* we already have the THEMED name and picture.
*/
elseif ($vars['revision_uid'] != '0') {
$edit_name = $vars['name'];
$edit_picture = $vars['user_picture'];
}
}
/* Adding the variables */
$vars['changed_name'] = $edit_name;
$vars['changed_picture'] = $edit_picture;
$vars['display_changed'] = $edit_display;

Thanks Caroline for the kickstart in the right direction!

I like that you can pick

I like that you can pick specific colors, it gives it soo much more character. Thanks for the great post and the great help!

These codes have attempted

These codes have attempted to apply quite a lot. I am a little confused to apply it, but I'll try it, I think it would be easier if the format to change the appearance of the code is not shaped like this. dermatologist kingwood

great

it can be edited easily for sure and yes I have to realize how important that is then to me... Thanks a lot for what you've done so far.. Los Angeles CA Electrician

I'm not an expert in this

I'm not an expert in this kind of data editing. I think it is quite difficult to do and makes me more confused at having to apply complex codes like this. New Jersey Sewer backup cleanup

I like...

I like that you can pick different specific colors, it gives it soo much more character and lets you personalize it more. Thanks for the great post and the great help!

Online Investment Advice

We indeed can easily see

We indeed can easily see that there are many people who spend a lot of time and money to become number one in Google. While you will be able to see many interesting things.

Dallas IT Consulting

Information should be true and valid enough before shared. It will be very danger to spread the lie information.
Dallas IT Consulting

Quite difficult to apply

Quite difficult to apply themes with codes like this. In my opinion, it is not easy to be done by a layman like me. If there was an easier way to do, of course I will try it.

No way....

To be honest – it was not possible to read all of it but I don’t think it was necessary to do so.
I got what I needed and with every single details. You surely owe me a thanks at least.

Nice Article

This looks definitely ideal, Really its amazing. You have some excellent articles but are getting many none appropriate feedback here too. I really like in a website, very useful, no pointless on reading. idateasia reviews||
idateasia.com reviews

useful tool!

This is a very useful tool! Using this tool the administrator can identify who has edited the node recently and at what time. By this we may be able to prevent malicious activities by hackers and the simple interface is an added advantage! hp-laptop-support.com

The page is not found in a

The page is not found in a site it could be that it was removed by the site owner so they can be one of the problems for its users that access it. painters in sacramento

Nice Stuff

Thats why I keep coming back for more

You really should specify

You really should specify what name you will use. There are many people who are looking for a name he could understand easily. Surely people would remember it. movie reviews for kids

thanks

I would like to thank very interesting post.

Beginning to understand Drupal

Drupal has never been my strong point but thanks to your tutorials I am beginning to understand some things, though I am trying to go step by step instead of wanting to learn everything at one go.

Good post. Never knew this,

Good post. Never knew this, thanks for letting me know. You make it entertaining and you still manage to keep it smart. This is truly a great blog.

thanks now I understand or

thanks now I understand or know more about it later when I would merkomendasikan to my friends

Good article with good ideas

Good article with good ideas and concepts, lots of great information and inspiration.

Thank you for sharing this

Thank you for sharing this article with us. I think this website really stands out, so much valuable content is hard to find.

well-written

Sant Ritz is also near elite schools such as St. Andrew's Secondary School and St. Andrew's Junior College. Cedar Girls' Secondary School and CedarPrimary School are also around in the area.

amazing writing

Your writing is simply amazing and I prefer to read quality content, I will gladly bookmark your site and come back for more.

Good thoughts and well-written

Several buses are available near Bartley Road and Upper Paya Lebar Road along with shopping centers and restaurants. Bartley Ridge is also near to Nex Shopping Centre as well as the buzzling Toa Payoh area. Entertainment for your loved ones and friends is therefore at your fingertips with the full condo facilities as well as the amenities in Bartley.
Bartley Condo

I am impressed.

Belgravia Villas is a new and upcoming cluster housing located in the Ang Mo Kio area, nested right in the Ang Mo Kio landed area. It is within a short drive to Little India, Orchard and city area. With expected completion in mid 2016, it comprises of 118 units in total with 100 units of terrace and 18 units of Semi-D. Future residents are within a short driving distance to Ang Ko Kio Hub and Compass Point. With such a short drive to the city area as well as the orchard and bugis area, entertainment for your love ones and family will come at a stone’s throw away.
Ang Mo Kio Cluster House

Ecopolitan EC has full and

Ecopolitan EC has full and unique facilities, which includes a guard house, clubhouse, Function Room & Indoor Gym Tennis Court, 50m Freeform Pool Pool Deck, Wading Pool, Splash Pool & Family Pool Jacuzzi & Hydro Spa, BBQ Area Dining and Play Fountain, Fitness Alcove & Children’s Playground and Garden Trail. The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Punggol.
Ecopolitan EC

A good blog always comes-up

A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.I have express a few of the articles on your website now, and I really like your style of blogging. I added it to my favorite’s blog site list and will be checking back soon…Please add good information that would help others in such good way.This post is exactly what I am interested. we need some more reliable information.

Does your writing fantasy

Does your writing fantasy inspired me, and now I write the blog. The real blog is spreading its wings rapidly. Is a good example. Thank you again for the free discussion of this appear on the network.

I have to say I am very

I have to say I am very impressed with the way you efficiently website and your posts are so informative. Thanks dude

I really think you will do

I really think you will do much better in the future I appreciate every thing you've added to information base.

Jewel at Buangkok will be

Jewel at Buangkok will be accessible via Buangkok MRT Station, which is just next to it. Commuting to the city area as well as the Serangoon area is therefore very convenient.
Jewel at Buangkok

The condo’s facilities

The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Bugis.
Several buses are available near Rocher Road and Beach Road along with shopping centers and restaurants.
DUO Residence

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <css> <html> <javascript> <mysql> <php> <span> <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <javascript>, <mysql>, <php>, <rails>, <ruby>.

More information about formatting options

CAPTCHA
I have to wonder if you're a human spammer or a machine, or less likely someone who cares to leave his or her thoughts behind.