dongqishou7471
2015-12-03 15:17 阅读 120
已采纳

Drupal自定义注册表单保存

I have written a custom module to add a simple form step before the user resistration form. I have the following which adds some custom fileds in to the registration form

function join_form_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'user_register_form') {
         //lots of extra fields
    }
}

I have also created an install script to create a new table in the DB so that I didn't have to mess with the default users table. Currently everything works, except saving the custom data to my new table.

Here is my custom submit function

function join_form_form_submit($form, &$form_state) {

    $form_id = $form_state['input']['form_id'];

    switch($form_id) {
        case 'join_form_form':
            //
            break;
        case 'user_register_form':
            $date = new DateTime();
            $data = array(
                'username' => $form_state['values']['name'],
                'email' => $form_state['values']['mail'],
                'title' => $form_state['values']['title'],
                'first_name' => $form_state['values']['first_name'],
                'last_name' => $form_state['values']['last_name'],
                'gender' => $form_state['values']['gender'],
                'dob' => $form_state['values']['dob'],
                'address_1' => $form_state['values']['address_1'],
                'address_2' => $form_state['values']['address_2'],
                'address_3' => $form_state['values']['address_3'],
                'country' => $form_state['values']['country'],
                'county_state' => $form_state['values']['county_state'],
                'postcode' => $form_state['values']['postcode'],
                'contact_pref' => $form_state['values']['contact'],
                'created' => $date->format('Y-m-d H:i:s')
            );

            user_save(drupal_anonymous_user(), $data);

            drupal_set_message(t('Good here.'));

            db_insert('registrations')
            ->fields(array(
                'title' => $form_state['values']['title'],
                'first_name' => $form_state['values']['first_name'],
                'last_name' => $form_state['values']['last_name'],
                'gender' => $form_state['values']['gender'],
                'dob' => $form_state['values']['dob'],
                'address_1' => $form_state['values']['address_1'],
                'address_2' => $form_state['values']['address_2'],
                'address_3' => $form_state['values']['address_3'],
                'country' => $form_state['values']['country'],
                'county_state' => $form_state['values']['county_state'],
                'postcode' => $form_state['values']['postcode'],
                'contact_pref' => $form_state['values']['contact'],
                'created' => $date->format('Y-m-d H:i:s')
            ))->execute();

            //empty session data
            unset($_SESSION['name']);
            unset($_SESSION['email']);

            break;
    }
}

Is it possible for me to override the default reg process to save the user normally and then save the data into my custom table and then define a redirect? Am I heading in the right direction?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douao3063 douao3063 2015-12-03 15:38

    You trying to use user_save for custom table, but this function works for users table and is define drupal function for users; My solution is:add the fields to users table in hook_update or when you installing the some module in hook_install and hook_schema_alter. This way is too easy. And than you can use $account = user_save(NULL, $data); Your account will content the new users info; So you can login your user like

            $form_state['uid'] = $account->uid;
            $form_state['redirect'] = '';
            user_login_submit(array(), $form_state);
    
    点赞 评论 复制链接分享

相关推荐