dongqishou7471 2015-12-03 15:17
浏览 123
已采纳

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 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?