开发者

Drupal: How to make a fieldset dependent using CTools

开发者 https://www.devze.com 2023-01-04 03:14 出处:网络
I am using Ctools Dependency to make a fieldset hideable. 开发者_运维问答This is part of my code:

I am using Ctools Dependency to make a fieldset hideable. 开发者_运维问答This is part of my code:

$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 above, There are two elements, one select and one fieldset. Both have #process and #dependency parameters and both point to one field for dependent value. Problem is elements like select or textfield can be hidden easily but it does not work for fieldset. In this support request page, CTools creator has mentioned that '#input' => true is a work around. As you see I added it to code, but it does not work as well.

Do you have any suggestion?


I found my answer after reading the source of CTools dependent. Fieldset should change as this:

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

First an ID must be set for he fieldset. Then it must be wrapped in a DIV tag. ID of the DIV should be feildset's ID suffixed with '-wrapper'.


now (Feb 2013) usage is:

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

And #process is no more needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消