2010-06-16 8 views
5

Ich benutze Ctools Dependency, um ein Fieldset verdeckt zu machen. Dies ist Teil meines Code:Drupal: Wie mache ich ein Fieldset abhängig von 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, 
); 

In Snippet oben, gibt es zwei Elemente, ein wählen und ein Fieldset. Beide haben # Prozess- und # Abhängigkeitsparameter und beide zeigen auf ein Feld für den abhängigen Wert. Problem ist, dass Elemente wie Select oder Textfield leicht versteckt werden können, aber nicht für Fieldset. In this Support-Anfrage Seite, hat CTools Creator erwähnt, dass '#input' => true ist ein Problem. Wie Sie sehen, habe ich es dem Code hinzugefügt, aber es funktioniert nicht so gut.

Haben Sie Vorschläge?

Antwort

5

Ich fand meine Antwort nach dem Lesen der Quelle von CTools abhängig. Der Feldsatz sollte folgendermaßen geändert werden:

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

Zuerst muss eine ID für das Feld festgelegt werden. Dann muss es in einem DIV-Tag verpackt werden. Die ID des DIV sollte die ID des Feildsets mit dem Zusatz '-wrapper' sein.

1

jetzt (Februar 2013) Nutzung ist:

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

Und #process wird nicht mehr benötigt.