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?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityAlthough 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.
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
}
}
if($account->uid && $account->original->status == 0 && $account->status == 1)
Jul 26, 2016 at 11:01
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).