%d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) { // find the next number available to append to the name $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1"; $nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$')); $namenew .= '_'. ($nameidx + 1); } } else { // One would expect a single implementation of the hook, but if there // are multiples out there use the last one $namenew = array_pop($names); } // replace with generated username db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid); $account->name = $namenew; break; } return; } /** * Implementation of hook_form_alter(). * */ function email_registration_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case 'user_register': if (isset($form['account']) && is_array($form['account'])) { $form['account']['name']['#type'] = 'hidden'; $form['account']['name']['#value'] = user_password(); $form['account']['mail']['#title'] = t('E-mail'); } else { $form['name']['#type'] = 'hidden'; $form['name']['#value'] = user_password(); $form['mail']['#title'] = t('E-mail'); } break; case 'user_pass': $form['name']['#title'] = t('E-mail'); $form['name']['#description'] = t('Enter your e-mail address. You\'ll be sent a new password immediately.'); break; case 'user_login': $form['name']['#title'] = t('E-mail'); $form['name']['#description'] = t('Enter your e-mail address.'); $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.'); $form['name']['#element_validate'][] = 'email_registration_user_login_validate'; break; case 'user_login_block': $form['name']['#title'] = t('E-mail'); $form['name']['#element_validate'][] = 'email_registration_user_login_validate'; break; } } /** * function email_registration_settings() { * return system_settings_form($form); * } */ /** * Custom validation function for user login form. * Allows users to authenticate by email only, which is our preferred method. * */ function email_registration_user_login_validate($form, &$form_state) { if (isset($form_state['values']['name'])) { if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']))) { $form_state['values']['name'] = $name; } } }