Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal: How to make a fieldset dependent using CTools

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?

like image 520
farzan Avatar asked Jun 16 '10 08:06

farzan


1 Answers

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'.

like image 169
farzan Avatar answered Nov 15 '22 07:11

farzan