gpt4 book ai didi

php - Drupal 7 hook_form_alter 和提交表单

转载 作者:行者123 更新时间:2023-11-29 05:12:12 26 4
gpt4 key购买 nike

我添加了一些自定义字段,以便在有人注册到该网站时我可以获得更多信息,但我想更改表单上的一个字段。我创建了一个具有 hook_form_alter 函数的模块,以便我可以更改注册表单的 field_first_name。这是代码

function user_registration_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'user_register_form'){

$form['field_first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#description' => t('Enter your first name.'),
'#maxlength' => 255,
'#required' => TRUE,
);



}
if (isset($form['actions']['submit'])) {
$form['actions']['submit']['#submit'][] ='user_registration_form_submit';
}

}

我还创建了一个处理表单提交的函数。

function user_registration_form_submit(&$form, &$form_state)
{


$fe_id = db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();
drupal_set_message(t('your form entry has been added'));
}

我的问题是,当我提交表单并检查用户详细信息时。我发现数据库中不存在名字详细信息,或者当我以管理员身份登录并单击“人员”链接时。我发现除了我试图更改的名字字段外,所有信息都已提交。我也试过在没有表单提交功能的情况下提交表单,但它仍然不起作用。

如果我添加 form_submit 函数,我会收到以下错误消息

Notice: Undefined index: value in user_registration_form_submit() (line 37 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).

PDOException: SQLSTATE[HY000]: General error: 1364 Field 'entity_id' doesn't have a default value: INSERT INTO {field_revision_field_first_name} (field_first_name_value) VALUES (:db_insert_placeholder_0); Array ( [:db_insert_placeholder_0] => ) in user_registration_form_submit() (line 38 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).

这是我的代码的第 37 和 38 行

'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();

我先在本地主机上创建模块,然后再将其推送到实时网站

最佳答案

首先,它是值(value),而不是值(value)。

为什么不使用提供此功能的模块,例如 profile2

用户不是在您的提交处理程序中创建的,因此您要么保存它:

function foo_user_register_form_submit($form, &$form_state){
$edit = array(
'name' => $form_state['values']['name'],
'pass' => user_password(),
'mail' => $form_state['values']['mail'],
'init' => $form_state['values']['mail'],
'status' => 1,
'access' => REQUEST_TIME,
'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
);
user_save(drupal_anonymous_user(), $edit);
}

有关详细信息,请参阅 docs .

或者您可以尝试在 form_alter 中添加自定义提交处理程序,该处理程序将在创建用户后触发:

$form['#submit'][] = 'your_custom_submit_handler_function';

应该已经在其中创建了用户。所以:

function your_custom_submit_handler_function(&$form, &$form_state)
{
$fe_id = db_insert('field_revision_field_first_name')
->fields(array(
'field_first_name_value' => $form_state['values']['field_first_name'],
))->execute();
drupal_set_message(t('your form entry has been added'));
}

But keep in mind, that this custom inserting maybe needs some more code, here you only add the revision entry ..

关于php - Drupal 7 hook_form_alter 和提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com