需要一个将字段集合附加到drupal表单的示例 - drupal 7
问题描述:
我有一些附加到节点类型的字段集合,我正在使用Drupal的Form API编写自定义表单。需要一个将字段集合附加到drupal表单的示例 - drupal 7
我该如何着手将这些字段集合字段加载到表单中?我应该使用hook_form
,hook_field
还是别的吗?
有人可以提供一个简单的例子,写一个表单元素是一个无限基数的字段集合?
答
我能够使用下面的代码来加载字段集合窗体。我添加了额外的注释,并删除了自$ form_state ['values']变量中的字段值的自定义逻辑。
function mymodule_custom_form($form, $form_state, $args){
$form = array();
$type = $args['type'];
$id = $args['id'];
module_load_include('inc', 'field_collection', 'field_collection.pages');
if(!empty($entity->field_comments[LANGUAGE_NONE][0]['value'])){
$field_collection_item = field_collection_item_load($entity->field_comments[$entity->language][0]['value'], TRUE);
$output = drupal_get_form('field_collection_item_form', $field_collection_item);
}else{
$output = field_collection_item_add('field_comments', 'node', $entity->nid);
}
dpm($output);
// If you want to use field collection form submit handlers as is, just return the below
// In case you want to use your own custom submit handlers then do modify the $output array as seems fit.
$form['field_collection_element'] = $output;
// You may also attach the $output array to your custom form array. Make sure you handle submit handlers properly
return $form;
}
样本提交处理 @see在提交处理程序,你可以使用here
function mymodule_custom_form_submit($form, $form_state){
...
if(empty($item_id)){
$field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name));
$field_collection_item->setHostEntity('node', $node);
}
else {
$items = entity_load('field_collection_item', array($item_id));
$field_collection_item = $items[$item_id];
}
if(is_object($field_collection_item)){
// for references.
$field_collection_item->field1[$node->language][0]['target_id'] = $field1_val;
// normal values
$field_collection_item->field2[$node->language][0]['value'] = $field2_val;
$field_collection_item->field3[$node->language][0]['value'] = $field3_val;
if(empty($item_id)){
$field_collection_item->save(FALSE);
}
else{
$field_collection_item->save(TRUE);
}
}
...
}
答
如果您正在寻找使用表格来填充内容的节点给出的例子中,你总是可以暴露节点形式给用户,hook_form_alter在用户访问的基础上隐藏元素。
或者,如果您正在寻找一个可视表格,您可以使用entityform,然后使用规则填充其他内容。
答
还有在Drupal形式分组域的另外一个概念叫
这使得格式化一组形式的项目。表单api页面上的示例适用于具有以下代码的联系人模块。
function contact_form_user_profile_form_alter(&$form, &$form_state) {
if ($form['#user_category'] == 'account') {
$account = $form['#user'];
$form['contact'] = array(
'#type' => 'fieldset',
'#title' => t('Contact settings'),
'#weight' => 5,
'#collapsible' => TRUE,
);
$form['contact']['contact'] = array(
'#type' => 'checkbox',
'#title' => t('Personal contact form'),
'#default_value' => !empty($account->data['contact']) ? $account->data['contact'] : FALSE,
'#description' => t('Allow other users to contact you via a <a href="@url">personal contact form</a> which keeps your e-mail address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.', array('@url' => url("user/$account->uid/contact"))),
);
}
}
因此,如果你的目的是只是一群各类togather领域那么这也是一个办法做到这一点。