This is a minor issue but it boggled my mind for a bit.
I noticed on my site that when users registered at /user/register they got the email intended for users created by an administrator. This was misleading, because it suggested they could log in when in reality we want them to contact us before approving their account. A few users became confused.
(Our account creation scheme is "Who can create accounts? Visitors, but administrator approval is required")
It turns out that I had made the following form_alter (a lot of code removed for clarity):
/*
* Implements hook_form_FORM_ID_alter().
*/
function customcode_form_user_register_form_alter(&$form, &$form_state, $form_id) {
...
if (isset($form['account']['notify'])) {
$form['account']['notify']['#default_value'] = TRUE;
}//if
...
}
I did this so that the people interacting with our userbase and creating accounts would not have to remember to tick "Notify user of new account".
But this broke the (convoluted) conditional logic in user_register_submit() in user.module (lines 3836-3865):
if ($admin && !$notify) {
drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
}
// No e-mail verification required; log in user immediately.
elseif (!$admin && !variable_get('user_email_verification', TRUE) && $account->status) {
...
drupal_set_message(t('Registration successful. You are now logged in.'));
...
}
// No administrator approval required.
elseif ($account->status || $notify) {
...
if ($notify) {
drupal_set_message(t('A welcome message with further instructions has been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
}
else {
drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));
$form_state['redirect'] = '';
}
}
// Administrator approval required.
else {
...
drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
...
}
Because notify was TRUE (line 2803 of the same file says $notify = !empty($form_state['values']['notify']);), it was sending the admin email.
This is minor because I did a form alter for it to happen. But I think that this form alter is pretty reasonable and that others may be doing similar things. Maybe I should have looked at user.module before adding this, but I'd rather have code work as I'd expect. It might also help to make the logic a little less crazy in that if statement - perhaps we could make use of the form flag administer_users that I'm now using in my form alter:
//don't do this on the normal user register form or it sends the wrong email
if ($form['administer_users']['#value'] == TRUE) {
$form['account']['notify']['#default_value'] = TRUE;
}
If anyone agrees I could write a patch for the logic but I won't if it turns out I'm just coming from a different code philosophy than you real Drupallers :)