3

On every creation of a node or edit of a node I need the "Authored by" field to be automatically populated with the username of anyone saving changes.

any ideas how I can do that?

Thanks.

  • Please elaborate, do you really want the author to change every time a node is edited? If so: do you need to keep the name of the original author, or is it OK if the name of the last editor overwrites the original author? – marcvangend Mar 6 '13 at 12:14
  • Sorry that wasn't clear, yes I want the author to change every time a node is edited. no problem if it overwrites the original author. – Ashkar A.Rahman Mar 6 '13 at 12:32
  • Look like this has been asked and answered stackoverflow.com/questions/4936968/…, drupal.stackexchange.com/questions/43936/…. – AdamG Mar 6 '13 at 12:52
  • Thanks for the Links Adam, would be quite useful if there is a solution in detail, I am very new Drupal. – Ashkar A.Rahman Mar 6 '13 at 13:57
6

Personally I think that a module like Rules (powerful and awesome as it is) is overkill for something relatively simple as this.

Looking at node_form(), I see that the author is set by populating the author field with the username. I would create a custom module which implements hook_form_BASE_FORM_ID_alter, and set the default value of the author name field to the name of the current user. That would look something like this (disclaimer: untested code!).

<?php

function MODULENAME_form_node_form_alter(&$form, &$form_state, $form_id) {
  // Always set the username to the current user. An empty string means anonymous.
  global $user;
  $form['author']['name']['#default_value'] = !empty($user->name) ? $user->name : '';
}

An advantage of this solution is that the field is populated in advance with a default value, allowing user with sufficient permissions (eg. administrators) to override the author as they always can. It would be harder to do that in a rules-based approach which only kicks in after the form has been submitted.


update:

The topic starter mentions in another answer that he ended up with the following code:

function module_name_form_alter(&$form, &$form_state, $form_id) {
  // Always set the username to the current user. An empty string means anonymous.
  global $user;
  if ($form_id == 'content_type_node_form') {
    $form['author']['name']['#default_value'] = $user->name;
  }
}

Some feedback on this code:

  • Of course, the if-statement makes sense if you only need this behavior in certain cases. You could even place the global $user; inside the if.
  • I would recommend that you keep the !empty($user->name) ? $user->name : '' part, instead of simply using $user->name. It may be an edge case, but as you can see in drupal_anonymous_user, the global $user object does not necessarily contain a 'name' property. If, at some point in the future, this form would become available for anonymous users, they will end up with php notices on their screen.
| improve this answer | |
3

I'd use Rules to do this. You need to create a rule to respond to a node being updated, then add an action which sets the author value from the current user.

| improve this answer | |
0

thanks a bunch, It worked well and I simply did this(just a few changes from what you've written)

function module_name_form_alter(&$form, &$form_state, $form_id) {
  // Always set the username to the current user. An empty string means anonymous.
  global $user;
  if($form_id=='content_type_node_form'){
    $form['author']['name']['#default_value'] = $user->name;
  }
}
| improve this answer | |
0

If we want to implement this in any node form irrespective of Content type then we can use preg_match.

function change_author_form_alter (&$form, &$form_state, $form_id)
{

global $user;
// for all type of content node forms we can use preg_match
if (preg_match('/.*_node_form/',$form_id)) {
$form['author']['name']['#default_value'] = $user->name ;
}
}
| improve this answer | |
-1

This should just happen when created, if Author and Date information are ticked in admin/structure/types/manage/[your_content_type], under display settings.

If you want something else, you can use the token module and use a token as default value ; again in the content type settings.

If you want it updated with every save, you have to use hook_node_update() in a custom module, and set $node->uid to current user's ID (global $user; $user->uid).

| improve this answer | |
  • 1
    I don't think this answers the question; the question says "populated with the username of anyone saving changes" By default, Drupal only sets the author when a node is created, but it doesn't update the author when the node is edited. – marcvangend Mar 6 '13 at 12:12
  • Hi rémy, Thanks for the reply, but that is done. My question is, how to automatically populate the username in the "Authored by field" – Ashkar A.Rahman Mar 6 '13 at 12:36

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.