我喜欢您添加整个表单并取消设置的解决方案。我从另一个角度攻击它——创建一个一次性的临时表单并仅复制您希望保留的字段。这是我在http://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/function/field_attach_form/7#comment-45908上发布的内容:
要将任意实体包中的单个字段(在本例中为自动完成节点引用文本字段)添加到另一个表单,请将表单创建为临时表单和表单状态,然后复制以放置该字段定义。就我而言,我正在处理 Commerce 结帐表格更改:
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 方法中的“目标”表单)。
感谢您分享您的解决方案!