6

I have some field collections that are attached to a node type and I am in the process of writing a custom form, using Drupal's Form API.

How do I go about loading these field collection fields into the form? Should I use hook_form, hook_field, or something else?

Can someone provide me a simple example of writing a form element that is a field collection with unlimited cardinality?

4

3 回答 3

0

我能够使用以下代码加载字段收集表单。我添加了额外的注释并删除了从 $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 在提交处理程序中,您可以使用此处给出的示例

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 );
    }
  }
  ...
}
于 2012-11-28T08:46:24.473 回答
0

如果您希望使用表单使用内容填充节点,则始终可以将节点表单公开给用户,并使用 hook_form_alter 以在用户访问的基础上隐藏元素。

或者,如果您正在寻找一个可字段的表单,您可以使用entityform,然后使用规则来填充其他内容。

于 2015-04-16T17:15:30.313 回答
0

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"))),
    );
  }
}

因此,如果您的意图是将各种字段组合在一起,那么这也是一种方法。

于 2015-06-11T03:56:29.447 回答