5

I need to change the revison uid of 7000 nodes on a site. So I am using Views Bulk Operations to perform this action, by publishing or saving the the 7000 nodes concerned. Then I use the hook_node_presave to update all contents. But unfortunately, the field revision_uid isn't properly updated. By cons the field log is properly updated..

$field_uid = field_get_items('node', $node, 'uid');
    if($field_uid){
         $node->log = $node->uid;        
         $node->revision_uid = $node->uid;
    }

The revision id is still the one I'm using to make this operation (1 to admin..). How can get around this problem??

5

You'll have to impersonate a user while performing your operation:

<?php
//Save current user data/session
global $user;
$original_user = $user;
$old_state = drupal_save_session(FALSE);

$field_uid = field_get_items('node', $node, 'uid');
    if($field_uid){
         // Load new $user data
         $user = user_load($node->uid);

         $node->log = $node->uid;        
         $node->revision_uid = $user->uid;
    }

// Fail safe - if your code fails session won't be saved
$user = $original_user;
//Rever to old user and save session
drupal_save_session($old_state);
?>

References:

| improve this answer | |
  • Thank you for your response. It seems correct. But actually I haven't tested it, because I solved my problem my using direct db_update.. – 118218 Sep 18 '12 at 23:39
3

I was able to solve my problem by using a direct db_update, here is my simple code:

function hook_node_update($node) {

  $node->log = $node->uid;
  $node->revision_uid = $node->uid;

  $revision_uid_updated = db_update('node_revision')
  ->fields(array(
    'uid' => $node->uid,
  ))
  ->condition('nid', $node->nid, '=')
  ->execute();

}

With this, I was able to reassign the revision ownership to the author of each content

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