0

Using Drupal 8 with quick_node_clone module.

When a node is cloned I need to change the 'owner' or 'authored by' value to the person who is cloning the node.

There is an opportunity of manually changing this but I need to automate this. I am have been trying the hook_form_alter with some alternatives, see the commented out section, without success.

function quick_node_clone_form_alter(&$form, FormStateInterface $form_state, $form_id) {

 print_r($form_id);  

 if ($form_id == 'quick_node_clone') {
  // change author
  //$form['author']['name']['#value'] = $user->uid;
  //$form->setOwnerId($current_user);
  //$form->uid = $user->uid;

  print_r($form['field_author']['nid']['nid']['#value']);  
  global $user;    
  $form['field_author']['nid']['nid']['#value'] == $user->uid;
 }
}

Any direction would be appreciated.

  • I think you need a presave hook rather than an alter hook... – cchen Oct 12 '18 at 17:47
0

Try this out:

use Drupal\Core\Entity\EntityInterface;
/**
 * Implements hook_entity_presave().
 */
function customModule_entity_presave(EntityInterface $entity) {
  $current_user_id = \Drupal::currentUser()->id();
  $entity->setOwnerId($current_user_id);
}

Edit
You would be better to check on your content type:

use Drupal\Core\Entity\EntityInterface;

/**
 * Implements hook_entity_presave().
 */
function customModule_entity_presave(EntityInterface $entity) {
  $current_user_id = \Drupal::currentUser()->id();
  if ($entity->bundle() == 'your_content_type_name'){
    $entity->setOwnerId($current_user_id);
  }
}
  • I appreciate the help but it doesn't seem to get the current_user_id into the Authoring Information Authored by field. And yes cache was cleared. Just to be clear for my understanding also. This is meant to trigger when the clone button is clicked. How does that trigger the _presave? – Dupls Oct 14 '18 at 20:01
  • i tested it works, for me, you did dump the $current_user_id variable its correct ? – berramou Oct 14 '18 at 20:03
  • Maybe I am missing something @berramou and appreciate your help. Here is what I have done. 1) in mymodule.module file I added this to the top use \Drupal\Core\Entity\EntityInterface 2) then added the code as you have it replacing customModule with mymodule. 3) Cleared cache and visited the admin/content page and from the edit Operations selected clone. 4) A cloned copy is created but the Author Information is still the original author. What am I missing? What did you mean by dump. – Dupls Oct 16 '18 at 16:02
  • in this hook print the $current_user_id just to see if the function executed and you get the right user id ? if you use devel you can use dpm($current_user_id); – berramou Oct 16 '18 at 16:07
  • i think the original other of the node you clone is the current user that's why you don't see the change. – berramou Oct 16 '18 at 16:18

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.