欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal 修改节点 编辑人的名字存起来

shiping1 的头像
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

shareimprove this question
 
  
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 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. –  DropDragon Mar 6 at 12:32
  
  
Thanks for the Links Adam, would be quite useful if there is a solution in detail, I am very new Drupal. –  DropDragon Mar 6 at 13:57 

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 implementshook_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 indrupal_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.
shareimprove this answer
  

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.

shareimprove this answer
  

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;
  }
}
shareimprove this answer
  

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).

shareimprove 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 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" –  DropDragon Mar 6 at 12:36


来自   http://drupal.stackexchange.com/questions/64609/authored-by-field-to-be-automatically-filled
普通分类: