Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access content profile CCK fields in Drupal registration form (user_register)

Tags:

profile

drupal

Here is my goal: Take Content profile CCK fields "field_firstname" and "field_lastname" to automagically generate a username.

My approach: To create a custom module using hook_form_altar and a submit handler to auto-append the username and not touch the db scheme.

My problem: I cannot find out how to access the Content Profile additional CCK fields on the registration form. Right now I have a live copy of this.. and I'm using var_dump($form) to look for fields. Live location here.

Here is my module code displaying, any pointers will be greatly appreciated

/**
 * Implementation of hook_form_alter().
 *
 */
function realname_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');
      }
      //$form['#submit'][] = 'realname_registration_name_submit';
      print "<pre>";
      print_r($form);
      print "</pre>";
      break;
  }
}


/**
 * submit handler
 *
 */
function realname_registration_name_submit($form, &$form_state) {

}
like image 882
plunder Avatar asked Dec 10 '25 08:12

plunder


1 Answers

The values that is submitted in a form, is always located at $form_state['values'][...]. If you have troubles figuring out what they are called, you can print out the $form_state variable and inspect it.

In your case, it looks like this is what you need:

$first_name = $form_state['values']['field_firstname'];
$last_name = $form_state['values']['field_lastname'];

One thing you need to be aware of, is that usernames need to be unique, while names might not be. You also need to expose the username for the new users, so they know how to login.

like image 79
googletorp Avatar answered Dec 12 '25 19:12

googletorp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!