2010-06-16 29 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。