2

I'm trying to use #states in form api to change the title of a fieldset based on his collapsed states.

so far here's my code :

$form['advanced'] = array(
  '#type' => 'fieldset',
  '#title' => t('Advanced search'),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  '#attributes' => array('class' => array('search-advanced', 'clearfix')),
  '#states' => array(
    '#title' => array(
      array('fieldset#edit-advanced' => array('#collapsed' => 'Hide advanced search')),
    ),
  ),
);

I've tried different things, but without success.

Thanks a lot.

2
  • 1
    I've never been able to get states to work with fieldsets; there's an unresolved issue on D.o about it but I can't find at the moment
    – Clive
    Commented Aug 20, 2013 at 9:25
  • 1
    @Clive There is a chain of duplicates and related that starts with this report. And this one seems to be exactly about OPs problem.
    – Mołot
    Commented Aug 20, 2013 at 9:41

1 Answer 1

3

It is true that there were some issues with #states and fieldsets, such as #states can't hide/show fieldsets, but in your case the code doesn't work because #title is not an handled state, and #collapsed is not a handled state for the remote condition.

The list of the implemented states is given in drupal_process_states():

  • enabled
  • disabled
  • required
  • optional
  • visible
  • invisible
  • checked
  • unchecked
  • expanded
  • collapsed

The states used for the remote conditions are the following:

  • empty
  • filled
  • checked
  • unchecked
  • expanded
  • collapsed
  • value

It would also be hard to implement that, as the state needs to be boolean, and the remove condition says the value of the state for the other form field.

For example, if foo is a select list, the state for a form field could be the following one.

'#states' => array(
  // Show the settings if 'bar' has been selected for 'foo'.
  'visible' => array(
    ':input[name="foo"]' => array('value' => 'bar'),
  ),
),

This state would make the form field visible when the value selected from the list is bar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.