5

我正在使用 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,
);

在上面的代码片段中,有两个元素,一个 select 和一个 fieldset。两者都有#process 和#dependency 参数,并且都指向一个字段以获取依赖值。问题是像 select 或 textfield 这样的元素可以很容易地隐藏,但它不适用于 fieldset。在支持请求页面中,CTools 创建者提到这'#input' => true是一种解决方法。如您所见,我将其添加到代码中,但效果不佳。

你有什么建议吗?

4

2 回答 2

5

在阅读了依赖 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 应该是 feildset 的 ID,后缀为“-wrapper”。

于 2010-06-16T13:06:49.130 回答
1

现在(2013 年 2 月)的用法是:

$form['foo_or_bar'] = array(
    '#title' => 'Foo or Bar',
    '#type' => 'radios',
    '#options' => array(
        "foo" => "Foo",
        "bar" => "Bar"
    ),
    '#default_value' => "foo",
);

$form['react_on_foo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Foo fieldset'),
    '#dependency' => array('radio:foo_or_bar' => array('foo')),
);

$form['react_on_foo']['dummy_for_foo_fieldset'] = array(
    '#title' => t('Dummy for FOO fieldset'),
    '#type' => 'textfield',
);


$form['react_on_bar'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bar fieldset'),
    '#dependency' => array('radio:foo_or_bar' => array('bar')),
);

$form['react_on_bar']['dummy_for_bar_fieldset'] = array(
    '#title' => t('Dummy for BAR fieldset'),
    '#type' => 'textfield',
);

并且不再需要#process。

于 2013-02-26T04:32:26.137 回答