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

查看:22
本文介绍了在 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_fieldfield_info_Fieldfield_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:

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

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 方法有很多未设置)以及源表单是否会随着时间的推移接收新字段(新字段将在 form-insert 方法中显示在您的目的地"表单上).

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天全站免登陆