2012-10-12 55 views
5

有与在Drupal 6 CCK的方法来附加CCK场在我们的自定义表单,如:CCK字段添加到自定义表单在Drupal 7

$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_Fieldfield_info_instance这样的函数,但它们不能被渲染为表单域。

+1

这是可能的,但很麻烦。你可以找到一个代码示例[这里](http://drupal.stackexchange.com/questions/25140/is-displaying-a-working-field-widget-form-on-its-own-possible) – Clive

回答

2

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

要将任何实体包(本例中为自动填充节点引用文本字段)的单个字段添加到另一个表单上,请将该表单创建为临时表单和formstate,然后将其复制到位该字段的定义。就我而言,我正在一个商务结算表单改变:

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

    .. 

的优势,无论是解决方案可能依赖于“源”的复杂形式(太复杂意味着大量取消设置与形式 - 插入方法)以及源表单是否会随着时间的推移接收新的字段(新的字段将在表单插入方法中显示在“目标”表单中)。

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

+0

谢谢这是真的有帮助! – miteshmap

+0

@texasbronius您能否帮助我将用户字段附加到我的自定义表单中?这里是[form.inc](https://www.dropbox.com/s/o1gf5j51n7jbo81/statuses.form.inc?dl=0)文件 – Umair

2

终于得到了答案。这是伎俩来做到这一点。

$node = new stdClass(); 
$node->type = 'video'; //content type 
field_attach_form('node', $node, $form, $form_state); 
unset($form['body']); //unset other fields like this. 

这将显示所有添加了字段api的自定义字段。因此您需要取消设置您不希望在表单中显示的任何额外字段。休息会像IT一样。

相关问题