Add address field to Signup form, save to Content Profile

Alright, so this a bit nuanced, but I thought that I'd share it anyway. Maybe this snippet will help someone somewhere.

Scenario

I'm using the Signup module to permit users to signup for events, but I'd like them to be able to enter more than the standard name and phone number. I'd like my users to be able to provide their address.

Beyond that, if the user has already provided their address on their user profile (generated using Content Profile and the Location CCK module), then I'd like the fields to prepopulate.

Furthermore (we're getting crazy here), if they enter a new address on the signup form, I'd like them to be able to save that address to their profile.

Whoa!

Actually, with the power of the Forms API, this becomes an easy task.

You'll clearly have to modify this script to match your specific CCK field names, but it should give you a good jumpstart!

uid); $form['signup_form_data']['#tree'] = TRUE; $form['signup_form_data']['name'] = array( '#type' => 'textfield', '#title' => t('Name'), '#size' => 40, '#maxlength' => 64, '#default_value' => $profile_node->field_name[0]['first'] . ' ' . $profile_node->field_name[0]['last'], ); $form['signup_form_data']['phone'] = array( '#type' => 'textfield', '#title' => t('Phone'), '#size' => 40, '#maxlength' => 64, '#default_value' => $profile_node->field_location[0]['phone'], ); $form['signup_form_data']['location'] = array ( '#type' => 'fieldset', '#title' => t('Location'), '#collapsible' => FALSE, '#collapsed' => FALSE, ); $form['signup_form_data']['location'] ['street'] = array( '#type' => 'textfield', '#title' => t('Street'), '#size' => 40, '#maxlength' => 64, '#default_value' => $profile_node->field_location[0]['street'], ); $form['signup_form_data']['location'] ['additional'] = array( '#type' => 'textfield', '#title' => t('Additional'), '#size' => 40, '#maxlength' => 64, '#default_value' => $profile_node->field_location[0]['additional'], ); $form['signup_form_data']['location'] ['province'] = array( '#type' => 'select', '#title' => t('State'), '#options' => array( 'AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona', 'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado', 'CT' => 'Connecticut', 'DE' => 'Delaware', 'DC' => 'District Of Columbia', 'FL' => 'Florida', 'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois', 'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky', 'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland', 'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota', 'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana', 'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire', 'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York', 'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio', 'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania', 'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota', 'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont', 'VA' => 'Virginia', 'WA' => 'Washington', 'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming', 'AS' => 'American Samoa', 'FM' => 'Federated States of Micronesia', 'GU' => 'Guam', 'MH' => 'Marshall Islands', 'MP' => 'Northern Mariana Islands', 'PW' => 'Palau', 'PR' => 'Puerto Rico', 'VI' => 'Virgin Islands' ), '#default_value' => $profile_node->field_location[0]['province'], ); $form['signup_form_data']['location']['postal_code'] = array( '#type' => 'textfield', '#title' => t('Zip'), '#size' => 40, '#maxlength' => 64, '#default_value' => $profile_node->field_location[0]['postal_code'], ); $form['signup_form_data']['save_address'] = array( '#type' => 'checkbox', '#title' => t('Save address to your profile?'), ); return $form; } function grasmash_form_alter(&$form, $form_state, $form_id) { //add additional validation function to form submit method if($form_id == 'signup_form'){ $form['#submit'][] = 'grasmash_signup_form_submit'; } } function grasmash_signup_form_submit(&$form, &$form_state) { //update user profile if ($form['#post']['signup_form_data']['save_address']){ global $user; $node = content_profile_load('profile', $user->uid); //update location fields (includes phone number) foreach($node->field_location[0] as $field_name => $value){ if ($form['#post']['signup_form_data'][$field_name]){ $node->field_location[0][$field_name] = $form['#post']['signup_form_data'][$field_name]; } } //save profile node if ($node = node_submit($node)) { node_save($node); drupal_set_message(t("Your profile has been updated.")); } else { drupal_set_message(t("There was an error updating your profile."), "error"); } } } ?>

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.