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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityOn 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.
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:
global $user;
inside the if.!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.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.
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;
}
}
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 ;
}
}
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
).