开发者

How can I combine fields to generate a username during registration?

In a custom module "nade_reg" I have changed the registration form to include two fields to collect the first and last-name of the user, while hiding the username field. I don't know if it's relevant to this, but I do have the login toboggan module enabled.

On submit, I want to the username to become Firstname Lastname (with the space)

function nade_reg_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    // This is our form ID.
    case 'user_register':        
     $form['name']['#type'] = 'hidden';

     $form['field_uprofile_first_name1'] = array(
       '#type' => 'textfield',
       '#title' => t('First Name'),
       '#weight' => -40,
       '#required' => TRUE,
     );

     $form['field_uprofile_last_name1'] = array(
       '#type' => 'textfield',
       '#title' => t('Last Name'),
       '#weight' => -35,
       '#required' => TRUE,
     );

     break;
  }
}

I've been trying to do this with a submission handler, but it seems that my function isn't being recognized by the system (even after much cache clearing), so can't tell where my errors are.

Is the code below correct, or do I need to do some wrangling with arrays?

Any ideas why this wouldn't be seen by the system?

function nade_reg_user_register_submit($form, &a开发者_如何学编程mp;$form_state) {
  drupal_set_message(t('function redlemonade_register_user_register_submit sucessfully called.'));

  $name = $form_state['values']['field_uprofile_first_name1'] . ' ' . $form_state['values']['field_uprofile_last_name1'];
}


I had a similar requirement where I wanted the username to always be equal to the email. Which I accomplished by hiding the username from the registration form as you are, and then using the user_presave() hook.

For you, it would look something like this;

function nade_reg_user_presave (&$edit, $account, $category) {

  // check that the names are being edited, or else you will overwrite 
  // with a blank username
  if (array_key_exists('first_name', $edit) && 
      array_key_exists('last_name', $edit)) {
    $edit['name'] = $edit['first_name'] . ' ' . $edit['last_name'];
  }

}

EDIT: for Drupal 6 per the comment (I think this will do the trick), I have used hook_user() to accomplish what I did above in D7, although it's a bit messier.

function nade_reg_user($op, &$edit, &$account, $category = NULL) {
  if (($op == 'insert' || $op == 'edit')  // if its the right operation
       && array_key_exists('first_name', $edit)  // and the right edits
       && array_key_exists('last_name', $edit)) {   
    $name = $edit['first_name'] . ' ' . $edit['last_name'];
    $qry = "UPDATE {users} SET name = '%s' WHERE uid = %d";
    if (db_query($qry, $name, $account->uid)) {
      $edit['name'] = $name; // update in the user array for access by other modules
    }
  }
}


Your submission handler doesn't work for existing forms, unless you specifiy an extra submit handler.

This can be achieved with : $form['#submit'][] = 'my_very_own_custom_submit_handler';

It's the same for adding extra validation handlers.

Alternatively, you could catch the user being inserted, through hook_user - as listed in a previous answer - which is the equivalent of hook_nodeapi, but for users. Changing the $edit variable should be enough during 'insert' op.


You need to set $form_state['values']['name'], not just $name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜