我正在使用 Ctools Dependency 使字段集可隐藏。这是我的代码的一部分:
$form['profile-status'] = array(
'#type' => 'radios',
'#title' => '',
'#options' => array(
'new' => t('Create a new profile.'),
'select' => t('Use an existing profile.'),
),
);
$form['select'] = array(
'#type' => 'select',
'#title' => t('Select a profile'),
'#options' => $options,
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:profile-status' => array('select')),
);
$form['profile-properties'] = array(
'#type' => 'fieldset',
'#title' => t('View the profile'),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:profile-status' => array('select')),
'#input' => true,
);
在上面的片段中,有两个元素,一个选择和一个字段集。两者都有#process 和#dependency 参数,并且都指向一个字段以获取相关值。问题是像 select 或 textfield 这样的元素可以很容易地隐藏,但它不适用于 fieldset。在 this支持请求页面,CTools 创建者提到
'#input' => true
是一种解决方法。如您所见,我将其添加到代码中,但效果不佳。你有什么建议吗?
最佳答案
我在阅读了依赖 CTools 的源代码后找到了答案。字段集应更改为:
$form['profile-properties'] = array(
'#type' => 'fieldset',
'#title' => t('View the profile'),
'#process' => array('ctools_dependent_process'),
'#dependency' => array('radio:profile-status' => array('select')),
'#input' => true,
'#id' => 'my-fs-id',
'#prefix' => '<div id="my-fs-id-wrapper">',
'#suffix' => '</div>',
);
首先必须为字段集设置一个 ID。然后它必须被包裹在一个 DIV 标签中。 DIV 的 ID 应该是带有“-wrapper”后缀的 feildset 的 ID。
关于Drupal:如何使用 CTools 使字段集依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3051697/