在Drupal 7中将CCK字段添加到自定义表单

mit*_*map 5 drupal cck drupal-7

在Drupal 6中有一个使用CCK的方法在我们的自定义表单中附加CCK字段,如:

$field = content_fields('field_name');  // field_name is cck field
(text_field,text_Area,image_field anything.)
$form['#field_info'][$name] = $field;
$form += content_field_form($form, $form_state, $field);
Run Code Online (Sandbox Code Playgroud)

如何在Drupal 7中实现相同的功能?我有一个表单,我想使用我为内容类型创建的字段.我查看了所有文件field.module但找不到任何内容.有它的功能,如_attach_field,field_info_Fieldfield_info_instance,但他们不能被渲染为一个表单字段.

tex*_*ius 2

我喜欢您添加整个表单并取消设置的解决方案。我从另一个角度攻击它——创建一个一次性的临时表单并仅复制您希望保留的字段。这是我在http://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/function/field_attach_form/7#comment-45908上发布的内容:

要将任意实体包(在本例中为自动完成节点引用文本字段)中的单个字段添加到另一个表单上,请将该表单创建为临时表单和表单状态,然后复制以放置该字段定义。就我而言,我正在修改商务结帐表格:

function example_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) {
  $tmpform = array();
  $tmpform_state = array();
  $tmpnode = new stdClass();
  $tmpnode->type = 'card';
  // Create the temporary form/state by reference
  field_attach_form('node', $tmpnode, $tmpform, $tmpform_state);
  // Create a new fieldset on the Commerce checkout form
  $form['cart_contents']['org_ref_wrap'] = array(
    '#type' => 'fieldset',
    '#title' => t('Support Organization'),
  );
  // Place a copy of the new form field within the new fieldset
  $form['cart_contents']['org_ref_wrap'][] = $tmpform['field_card_organization'];
  // Copy over the $form_state field element as well to avoid Undefined index notices
  $form_state['field']['field_card_organization'] = $tmpform_state['field']['field_card_organization'];

  ..
Run Code Online (Sandbox Code Playgroud)

任一解决方案的优点可能取决于“源”表单的复杂性(太复杂意味着表单插入方法有很多未设置)以及源表单是否会随着时间的推移接收新字段(新字段将显示在您在表单插入方法中的“目标”表单)。

感谢您分享您的解决方案!