需要一个关于将字段集合附加到drupal表单的示例 - drupal 7

时间:2012-11-13 16:45:01

标签: drupal drupal-7 drupal-modules

我有一些附加到节点类型的字段集合,我正在使用Drupal的Form API编写自定义表单。

如何将这些字段集合字段加载到表单中?我应该使用hook_formhook_field还是其他?

有人能为我提供一个简单的例子来编写一个表单元素,它是一个具有无限基数的字段集合吗?

3 个答案:

答案 0 :(得分: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在提交处理程序中,您可以使用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 );
    }
  }
  ...
}

答案 1 :(得分:0)

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

或者,如果您要查找可现场表单,可以使用entityform,然后使用规则填充其他内容。

答案 2 :(得分:0)

Drupal表单中还有另一个分组字段的概念,叫做

  

Fieldset

这允许格式化一组表单项。表单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"))),
    );
  }
}

因此,如果您打算将各种领域分组,那么这也是一种方法。