在Drupal 7中将CCK字段添加到自定义表单 [英] Add CCK field to custom form in Drupal 7

查看:109
本文介绍了在Drupal 7中将CCK字段添加到自定义表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

There was a method with CCK in Drupal 6 to attach a CCK field in our custom form, like:

$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);

如何在Drupal 7中实现相同的功能?我有一个表单,我想使用我为内容类型创建的字段。我浏览了 field.module 的所有文件,但找不到任何内容。有一些功能就像 _attach_field field_info_Field field_info_instance ,但它们不能被渲染为一个表单域。

How can I achieve the same functionality in Drupal 7? I had a form and I want to use the field that I created for a content type. I went through all the files of field.module but couldn't find anything. There are functions in it like _attach_field, field_info_Field and field_info_instance, but they can't be rendered as a form field.

推荐答案

我喜欢您添加整个表单和解除设置的解决方案。我从另一个角度攻击它 - 创建一个抛弃的临时表单,只复制你想保存的字段。以下是我在发布的内容http://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/function/field_attach_form/7#comment-45908

I like your solution of adding the entire form and unsetting. I am attacking it from another angle-- create a throw-away temporary form and copy in only the field(s) you wish to preserve. Here's what I posted at http://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/function/field_attach_form/7#comment-45908:

要将任意实体包(在这种情况下,自动完成重新引导文本域)中单个字段添加到另一个表单上,请将表单创建为临时表单和formstate,并复制到该字段定义中。在我的情况下,我正在使用商业结算表格alter:

To add a single field off an arbitrary entity bundle (in this case, an autocomplete nodereference textfield) onto another form, create the form as a temporary form and formstate, and copy in to place that field definition. In my case, I'm working on a Commerce checkout form alter:

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'];

  ..

任一解决方案的优势可能取决于源形式(太复杂意味着使用form-insert方法的大量unset),以及源表单是否会随着时间的推移而接收到新的字段(新的字段将以目的地形式显示在表单插入方法中)

The advantage to either solution likely depends on the complexity of the "source" form (too complex means a lot of unsets with the form-insert method) and whether the source form will ever receive new fields over time (new fields will show up on your "destination" form in the form-insert method).

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

Thanks for sharing your solution!

这篇关于在Drupal 7中将CCK字段添加到自定义表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆