8

I want to add a content type after a user registers to the site.

I tried hook_user_save() but it saves the content type before the user confirms his e-mail.

Is there a hook after e-mail confirmation?

3 Answers 3

11

Although this approach uses modules, I add nodes after users confirm their e-mails using Logintoboggan and Rules. The Logintoboggan rules integration adds a new event, When the user account is validated, which will allow you to perform actions upon e-mail confirmation.

1
  • Be sure to install the LoginToboggan Rules Integration module from the LoginToboggan project.
    – Batandwa
    Aug 21, 2014 at 0:02
8

This does the job for me:

/**
 * Implements @see hook_user_presave
 */
function hook_user_presave(&$edit, $account, $category) {
  if ($account->uid // user is not new
      && $account->status === "0" && $edit['status']==1) { // user is being activated
  }
}
2
  • I had to use if($account->uid && $account->original->status == 0 && $account->status == 1)
    – Sébastien
    Jul 26, 2016 at 11:01
  • This doesn't seem to be working in Drupal 9 anymore. When the user clicks on the one-time link and saves his/her account after filling the password, the original status is already 1. May 31, 2022 at 8:03
2

If you're using the LoginToboggan module for e-mail validation and you don't wish to use the rules module you can simply mimic the module's validation response (exploiting a temporary logintoboggan_email_validated = TRUE account property that's pushed to hook_user_update) yourself in code:

 /**
 * Implement hook_user_update()
 *
 */
function yourcustommodule_user_update(&$edit, $account) {
  if (!empty($account->logintoboggan_email_validated) && !isset($account->your_custom_action)) {
        $account->your_custom_action = TRUE;

        // Do what you want here

  }
}

Since core and other modules will also invoke hook_user_update you'd want to implement something to avoid repeated actions. In this example I set another property on the $account once the action is initiated but you can impose finer control if necessary.

Note that if using LoginToboggan for automatic e-mail validation IOco's method won't work (among the many reasons - during a hook_user_presave, the $account->status == 1 (it's just the role is in your elected "pre-authorised" state).

Your Answer

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

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