I just started using logintoboggan and I am very happy with it. However, I do miss a feature that is present in Email Registration : Automatic username creation. It is quite difficulult for users to guess the proper name to use so that it is unique. OTOH, e-mails are unique by nature. BUT, usernames are displayed in various parts of the system, whereas e-mails should be confidential. So, I needed a way to generate them automatically.

I got the basic structure from Email registration, and fixed it a little. I am submitting it here in case the author is interested in adding this functionality to the module. The code is reasonably tested, and works... Of course, for proper integration, a config option would need to be created, to activate this feature on demand. Also, I am submitting the snippet in case somebody wants to add it to their custom module, as I did. Anyway, here goes:


function is_admin(){
  global $user;
  return ($user && $user->uid && in_array('administrator',$user->roles));
}

function name_generate($form, &$form_state) {
  $name=trim(@$form_state['values']['name']);
  if ($name=='___MAKENEW___') {
    $mail=@$form_state['values']['mail'];
    if (!$mail) return;
    $namenew = preg_replace('/@.*$/', '',$mail);
    $found=db_query("SELECT 1 FROM {users} WHERE LOWER(name) = LOWER(:namenew)",
           array(':namenew' => $namenew))->fetchField();
    if ($found){
      $nameidx=db_query_range("SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users}
        WHERE name REGEXP :search
        ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC",
        0, 1,
        array(':search' => '^' . $namenew . '_[0-9]+$'))->fetchField();
      $namenew .= '_' . ($nameidx + 1);
    }
    $form_state['values']['name']=$namenew;
  }
}

function slg_ext_form_user_register_form_alter(&$form, &$form_state){
  if (\\SLG_EXT\\is_admin()) return;
  if (array_key_exists('account',$form)) $ref=& $form['account']['name']; else $ref=& $form['name'];
  $ref['#type'] = 'hidden';
  $ref['#value'] = '___MAKENEW___'; //field is required
  $ref['#element_validate'][] = 'SLG_EXT\\name_generate';
}

as you can see, I am quite loose with naming, because I use namespaced code, so I dont have to worry about naming conflicts. if you decide to use this code, adjust names accordingly.

PS it seems (after previewing) that this site messes up namespaced code when syntax-highlighting,
so thing that look like SLG_EXTis_admin are actually : \SLG_EXT\is_admin. well, namespaces are sort of new, and not everything supports them. They ARE great, however, when you have control of your site (so you can force a namespace-aware version of PHP), to keep things clean...

Comments

murz’s picture

Thanks for the patch, I need registration without username too. Will be good to see this in core.

stevecowie’s picture

Status: Active » Closed (works as designed)

Thanks for submitting the code, but as email registration is already out there, I don't see a compelling case for adding this to Login Toboggan.