2010-06-16 59 views
5

我正在使用Ctools依赖关系来使字段集隐藏起来。这是我的代码的一部分:Drupal:如何使用CTools依赖现场集合

$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。在this支持请求页面,CTools创建者已经提到'#input' => true是一个工作。正如你看到的,我将它添加到代码中,但它不工作。

你有什么建议吗?

回答

5

我在阅读CTools相关来源后找到了我的答案。 Fieldset应该更改为:

$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>', 
); 

首先必须为fieldset设置一个ID。然后它必须包裹在DIV标签中。 DIV的ID应该是feildset的ID后缀'-wrapper'。

1

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

$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。