Tim*_*Tim 3 overriding drupal registration
我希望能够在Drupal 6中自定义用户注册表单
我已经找到了成千上万的教程,告诉我如何覆盖你可以输出整个表单的结构,但我想移动表单元素等等我似乎很难看到最好的方法来做到这一点
小智 9
为了扩展Jeremy的答案,你将要学习Drupal的Form API和user_register().简而言之,您构建了一个关联的数组; 数组中的每个元素对应一个表单元素.
数组中的每个表单元素都是自己的关联数组.它们可以有一个类型:文本字段,选择菜单,复选框等:请参阅所有类型的Form API参考.
每个表单元素也可以具有权重:这是您对元素进行排序的方式.较低编号的权重显示在表单中较高编号的权重之前.
您可以使用的元素类型之一是fieldset:这将允许您将元素组合在一起.使用字段集时,它会使用自己的权重值创建表单的一部分.
所以,假设您有一个包含三个字段的表单:姓名,公司和电子邮件地址.该名称应显示第一,公司第二,E-mail地址第三.您可以像这样指定表单:
$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,
);
Run Code Online (Sandbox Code Playgroud)
注意#weight钥匙.如果您希望公司出现在电子邮件地址之后,您将设置$form['company']['#weight']为高于3的值.
现在假设你想将Name和Company分组到一个名为Personal Information的字段集中.您的表单现在看起来像这样:
$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,
);
Run Code Online (Sandbox Code Playgroud)
请注意,Name和Company现在是数组元素$form['personal'].
如果你想使名称显示出来后,公司在字段集,设置其#weight高于2.由于名称现在是具有较低的一个字段的部分#weight比E-mail地址场,即使你设定$form['personal']['name']['#weight']为4,它止跌不要在电子邮件地址后显示名称.
因此,您要尝试做的是使用hook_form_alter()更改user_register表单来更改某些表单元素的权重,创建自己的字段集,以及将某些表单元素移动到新创建的字段集中.
有很多方法可以在您的主题中执行此操作,但我更喜欢为此创建自定义模块.创建自定义模块,并实现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']);
}
}
Run Code Online (Sandbox Code Playgroud)
在更复杂的形式中,将事物从一个字段集移动到另一个字段集可能会在提交表单时产生意外结果.这是因为$form['name']不一样$form['group']['name'],不一样$form['other_group']['name'].user_register在大多数情况下,您不必在表单上担心这一点,但请查看#tree和#parents上的手册页以获取有关此内容的更多信息.
这包括修改用户注册表单中的现有字段:如果要添加新字段,我强烈建议您使用内容配置文件.如果您想自己创建自定义字段,那么您将需要实现自己的验证和提交处理程序,这将变得更加复杂.内容配置文件为您处理此问题:查看其自述文件以了解如何为注册表单激活它.