0

Drupal Version: 8.92

Part 1. I am trying to create a node when the user is activated by the administrator for the first time. However, I am not able to find the right event/hook to execute the code. I tried "user_activated" but it is not working.

function my_module_user_activated(\Drupal\user\UserInterface $user) {
  \Drupal\node\Entity\Node::create([
    'type' => 'my_node',
    'title' => $user->getDisplayName(),
    'uid' => $user->id(),
  ])->save();
}

Any pointers?

PS: I don't want to create the node using hook_user_insert().

Part 2. If I need to delay the node creation to user validating the email by clicking a URL, what hook should I try?

Thank You! Uri

4

1 Answer 1

1

Modified code based on the pointers from No Sssweat. The working code is below for any future references.

function <MY-MODULE>_user_presave(\Drupal\user\UserInterface $user) {
  if (($user->isActive()) and ($user->getLastAccessedTime() == 0 ) and ( $user->hasRole('role_name'))){
  \Drupal\node\Entity\Node::create([
    'type' => 'my_content_type',
    'title' => $user->getDisplayName(),
    'uid' => $user->id(),
  ])->save();
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.