Php Drupal 7中的条件字段组/字段集
背景:在Drupal7中,我用CCK创建了一个表单(也称为字段UI)。我使用该模块创建了一个字段组,但我需要它是有条件的,这意味着它将仅根据以前的答案显示 以前的研究:要创建条件字段,可以使用编辑属性,如下所示:Php Drupal 7中的条件字段组/字段集,php,drupal,drupal-7,Php,Drupal,Drupal 7,背景:在Drupal7中,我用CCK创建了一个表单(也称为字段UI)。我使用该模块创建了一个字段组,但我需要它是有条件的,这意味着它将仅根据以前的答案显示 以前的研究:要创建条件字段,可以使用编辑属性,如下所示: function MYMODULE_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'person_info_node_form') { // Display 'field_maide
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'person_info_node_form') {
// Display 'field_maiden_name' only if married
$form['field_maiden_name']['#states'] = array(
'visible' => array(
':input[name="field_married[und]"]' => array('value' => 'Yes'),
),
);
}
}
然而,似乎没有办法为字段组使用States API。需要注意的一点是,虽然字段存储在$form
中,但字段组存储在$form['#groups']
以及$form['#fieldgroups']
中。我不知道如何区分这些,考虑到这一点,我尝试以与上面相同的方式将属性应用于fieldgroup。但是,它只会产生服务器错误
问题:有没有办法使用States API或其他替代方法有条件地显示字段组?也许您可以尝试查看的代码来帮助您找到想法。以下是我提出的最简单的解决方案。这主要有两个部分:(1)以编程方式更改表单的显示,(2)使用GUI更改内容的显示 (1.)首先,我使用编程方式创建条件字段集并向其中添加现有字段。代码如下所示
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'FORM_ID_node_form') {
// programmatically create a conditional fieldset
$form['MYFIELDSET'] = array( // do NOT name the same as a 'Field group' fieldset or problems will occur
'#type' => 'fieldset',
'#title' => t('Conditional fieldset'),
'#weight' => intval($form['field_PARENT']['#weight'])+1, // put this fieldset right after it's "parent" field
'#states' => array(
'visible' => array(
':input[name="field_PARENT[und]"]' => array('value' => 'Yes'), // only show if field_PARENT == 'Yes'
),
),
);
// add existing fields (created with the Field UI) to the
// conditional fieldset
$fields = array('field_MYFIELD1', 'field_MYFIELD2', 'field_MYFIELD3');
$form = MYMODULE_addToFieldset($form, 'MYFIELDSET', $fields);
}
}
/**
* Adds existing fields to the specified fieldset.
*
* @param array $form Nested array of form elements that comprise the form.
* @param string $fieldset The machine name of the fieldset.
* @param array $fields An array of the machine names of all fields to
* be included in the fieldset.
* @return array $form The updated form.
*/
function MYMODULE_addToFieldSet($form, $fieldset, $fields) {
foreach($fields as $field) {
$form[$fieldset][$field] = $form[$field]; // copy existing field into fieldset
unset($form[$field]); // destroy the original field or duplication will occur
}
return $form;
}
(2.)然后我使用模块来改变内容的显示。我通过转到我的内容类型并使用“管理显示”选项卡创建一个字段组并将我的字段添加到其中来实现这一点。这样,表单和保存的内容上的字段将显示为同一组的一部分。您必须使用
简单地说:
function your_module_field_group_build_pre_render_alter(&$element) {
$element['your_field_group']['#states'] = array(
'visible' => array(
':input[name="field_checkbox"]' => array('checked' => TRUE),
),
);
}
这很有效。如果A组在另一个组中,请执行此操作
$element['groupA']['groupB']['#states'] etc....
如果不存在id属性,则可能需要添加id属性:
$element['your_field_group']['#attributes']['id'] = 'some-id';
$element['yout_field_group']['#id'] = 'some-id';
谢谢你的提示。事实上,我并没有看到它,因为我终于找到了工作。如果您感兴趣,请查看发布的解决方案。哇,这比我的解决方案优雅得多!非常感谢。这种方法对我很有效,但我遇到了一个大问题。创建不同内容类型的节点时,仍将执行此代码。当我试图在代码中加入一个条件来检查特定的节点类型时,$元素似乎将值从另一个形式转移到了这个形式。主要问题:节点类型不正确。还有其他人经历过吗?我在D7上。@MTsrb在
$element['#form_id']