gpt4 book ai didi

drupal - 在 Drupal 6 中覆盖用户注册表单

转载 作者:行者123 更新时间:2023-12-01 07:22:10 25 4
gpt4 key购买 nike

我希望能够在 Drupal 6 中自定义用户注册表单

我找到了数千个教程,这些教程向我展示了如何覆盖可以将表单作为一个整体输出的结构,但我想移动表单元素等,我似乎看不到最好的方法

最佳答案

要扩展 Jeremy 的答案,您需要学习 Drupal 的 Form API user_register() .简而言之,您构建了一个关联数组;数组中的每个元素对应一个表单元素。

数组中的每个表单元素都是其自己的关联数组。它们可以有一个类型:文本字段、选择菜单、复选框等:参见 Form API reference对于所有类型。

每个表单元素也可以有一个权重:这就是您对元素进行排序的方式。较低编号的权重显示在表单中较高编号的权重之前。

您可以使用的元素类型之一是 fieldset :这将允许您将元素组合在一起。当您使用字段集时,它会使用自己的权重值创建表单的一部分。

因此,假设您有一个包含三个字段的表单:姓名、公司和电子邮件地址。名称应首先显示,公司第二,电子邮件地址第三。您可以像这样指定表单:

$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);

注意 #weight key 。如果您希望公司出现在电子邮件地址之后,您可以设置 $form['company']['#weight']高于3的东西。

现在假设您想将姓名和公司分组到一个名为“个人信息”的字段集中。您的表单现在看起来像这样:
$form['personal'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
'#weight' => 1,
);
$form['personal']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#weight' => 1,
);
$form['personal']['company'] = array(
'#type' => 'textfield',
'#title' => t('Company'),
'#weight' => 2,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail address'),
'#weight' => 3,
);

注意 Name 和 Company 现在是 $form['personal'] 的数组元素。 .

如果您想让 Name 在字段集中显示在 Company 之后,请将其设置为 #weight高于 2。因为名称现在是具有较低 #weight 的字段集的一部分。比电子邮件地址字段,即使您设置 $form['personal']['name']['#weight']到4,它不会使名称显示在电子邮件地址之后。

因此,您将尝试使用 hook_form_alter() 更改 user_register form 更改某些表单元素的权重,创建您自己的字段集,并将某些表单元素移动到您新创建的字段集中。

在你的主题中有很多方法可以做到这一点,但我更喜欢为此创建一个自定义模块。创建您的自定义模块,并实现 hook_form_alter() :
function test_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'user_register') { // Only modify the user registration form
// Before you can get down to business, you need to figure out the
// structure of the user registration form. Use var_dump or kpr to dump
// the $form array.

// Note: if you want to use kpr on the user registration form, give
// anonymous permission to see devel information.
// kpr($form);

// Move Name field to after E-Mail field
$form['name']['#weight'] = 2;
$form['mail']['#weight'] = 1;

// Group Name and E-mail together into a fieldset
$form['personal_info'] = array(
'#type' => 'fieldset',
'#title' => t('Personal information'),
);

$form['personal_info']['name'] = $form['name'];
$form['personal_info']['mail'] = $form['mail'];

// The last block only copied the elements: unset the old ones.
unset($form['name']);
unset($form['mail']);
}
}

在更复杂的表单中,将内容从一个字段集移动到另一个字段集可能会在提交表单时产生意想不到的结果。这是因为 $form['name']$form['group']['name'] 不同,与 $form['other_group']['name'] 不同.您不必担心 user_register大多数情况下,请查看 #tree and #parents 上的手册页面有关这方面的更多信息。

这包括修改用户注册表单中的现有字段:如果要添加新字段,我强烈建议使用 Content Profile .如果您想自己创建自定义字段,它将变得更加复杂,因为您将不得不实现自己的验证和提交处理程序。 Content Profile 为您处理这个问题:查看它的 README 以了解如何为注册表激活它。

关于drupal - 在 Drupal 6 中覆盖用户注册表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3386885/

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