4

I would like to keep info on the user that last edited a node. From a little search I found that you can easily obtain that info from the node_revisions table. But what if you don't want to enable revisions? Is there any other way to get the uid of the user?

I thought of manually adding a field to my content type that will be set to the current's user username. So every time a user hits save that field will store his info. But then I would have to add field permissions to make sure the users don't see or edit this field.

I was hoping for a more out-of-the-box solution. Probably there isn't any otherwise google would have given me the answer. All ideas appreciated. Thank you

3

You can do that by implementing hook_node_presave in your custom module, by something like:

function MODULE_node_presave($node) {
  $recent_copy = array_shift(array_values((node_revision_list($node))));
  drupal_set_message('User id of the user who edited latest is '.var_export($recent_copy->uid, TRUE));
  drupal_set_message('Username of the user who edited latest is '.var_export($recent_copy->name, TRUE));
}

You can find a detailed solution see: Get the edited node details without enabling node revision in Drupal.

| improve this answer | |
  • I see you are using node_revision_list. Doesn't that require that I enable creating a new revision every time the node is saved? If that is the case I was hoping to avoid creating revisions for nodes when all I want to save is the uid. – Perisdr Nov 26 '12 at 15:07
  • @Perisdr nope, that doesn't require to enable revision. I've tested the above without revisioning enabled, and it worked :) – AjitS Nov 26 '12 at 15:10
1

In case you are interested ONLY on the last editor and you are ok to lose the information about who was the original author you can substitute the Author reference with the user that has last edited the node:

  • Install and enable the Rules module
  • Create a rule that works "After updating existing content of type <your content type>"
  • Add and Action of type "Set a data value"
    • parameter to be set: [node:author]
    • value to be set: [site:current-user]

After this you can show the Author and it will display the last editor instead.

Note: this solution it is not intended to be ok in all the scenario but it has pros and cons that in more than few cases makes it valuable:

Cons:

  • losing the original Author information

Pros:

  • no need to enable revisions (so saving database usage)
  • no need to do database queries
  • no need to change the template files
| improve this answer | |
0

There is a thread on the drupal site about this: http://drupal.org/node/81242

You will have to run a query from your template file, and it will then return the last username and a link to his/her profile.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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