Drupal 自定义模块/表单问题:添加字段数组
我正在创建一个自定义模块,我想在其中为特定字段提供“添加另一个项目”功能,但我似乎无法弄清楚我需要做什么才能完成此任务......我'我一直在浏览 Drupal 论坛及其 Forms API 参考,但我一定没有得到任何东西......我正在使用 Drupal 6.20,在我的模块中,我尝试了:
$form['options'] = array(
'#title' => t('Options'),
'#type' => 'fieldset',
);
$form['options']['address'] = array(
'#type'=>'textfield',
'#title'=>t('Address'),
'#tree' => 1,
);
我想我会得到一个如下所示的文本输入:
<input type="text" class="form-text text" value="" size="60" id="edit-address-0-value" name="address[0][value]">
但是,我只是得到一个看起来像的输入 这:
<input type="text" class="form-text" value="" size="60" id="edit-address" name="address" maxlength="128">
I'm creating a custom module where I'd like to have the "add another item" functionality for a particular field, but I can't seem to figure out what I need to do in order to accomplish this... I've been going through Drupal forums and their Forms API Reference, but I must not be getting something.... I'm using Drupal 6.20, and in my module, I tried:
$form['options'] = array(
'#title' => t('Options'),
'#type' => 'fieldset',
);
$form['options']['address'] = array(
'#type'=>'textfield',
'#title'=>t('Address'),
'#tree' => 1,
);
Thinking I would get an text input that looked like this:
<input type="text" class="form-text text" value="" size="60" id="edit-address-0-value" name="address[0][value]">
But, I just get an input that looks like this:
<input type="text" class="form-text" value="" size="60" id="edit-address" name="address" maxlength="128">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在要复制的元素上方的元素上设置#tree。 FAPI 将从该元素向下将值存储在树结构中。
You need to set #tree on the element above the one you want to duplicate. FAPI will store values in a tree structure from that element on downwards.
要获得像
address[0][value]
这样的名称,您将需要类似的内容,但您不需要
[value]
部分,除非您实际上正在尝试实现多个-值分组字段,或者如果您的字段具有使用多个 PHP 值(即纬度/经度、开始/停止日期等)实现的复杂(自定义)数据类型。您可能还需要将值的数量存储在
$form['options']['#nb_values']
或隐藏字段中(如果您打算将其他字段添加到使用 JavaScript 形成)。To get a name like
address[0][value]
you will need something likeBut you don't need the
[value]
part unless you are actually trying to achieve multi-valued grouped fields or if your field has a complex (custom) data type implemented with multiple PHP values (ie. latitude/longitude, start/stop dates, etc.).You will also probably need to store the number of values in something like
$form['options']['#nb_values']
or in an hidden field (if you plan to add the additional fields to the form using JavaScript).