Drupal自定义注册表单保存

时间:2015-12-03 15:17:37

标签: php drupal drupal-7

我编写了一个自定义模块,用于在用户注册表单之前添加一个简单的表单步骤。我有以下内容,将一些自定义文件添加到注册表单

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

我还创建了一个安装脚本来在DB中创建一个新表,这样我就不必弄乱默认用户表了。目前一切正常,除了将自定义数据保存到我的新表。

这是我的自定义提交功能

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;
    }
}

是否可以覆盖默认的reg进程以正常保存用户,然后将数据保存到我的自定义表中,然后定义重定向?我正朝着正确的方向前进吗?

1 个答案:

答案 0 :(得分:0)

您尝试将user_save用于自定义表,但此函数适用于users表,并为用户定义drupal函数; 我的解决方案是:在hook_update中将字段添加到users表,或者在hook_install和hook_schema_alter中安装某个模块。这种方式太容易了。并且您可以使用$account = user_save(NULL, $data);您的帐户将满足新用户信息;所以你可以像

那样登录你的用户
        $form_state['uid'] = $account->uid;
        $form_state['redirect'] = '';
        user_login_submit(array(), $form_state);