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);
如何在 Drupal 7 中实现相同的功能?我有一个表单,我想使用为内容类型创建的字段。我浏览了所有文件field.module
但找不到任何东西。里面有类似的功能_attach_field
, field_info_Field
and field_info_instance
,但它们不能呈现为表单字段。
我喜欢您添加整个表单并取消设置的解决方案。我从另一个角度攻击它——创建一个一次性的临时表单并仅复制您希望保留的字段。这是我发布的内容http://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/function/field_attach_form/7#comment-45908 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'];
..
任一解决方案的优点可能取决于“源”表单的复杂性(太复杂意味着表单插入方法有很多未设置)以及源表单是否会随着时间的推移接收新字段(新字段将显示在表单插入方法中的“目标”表单)。
感谢您分享您的解决方案!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)