I'm working on a Drupal 7 project where I use the form API to build a form. I want my form's fieldsets to be collapsed by default.

I use the following code:

$form['fieldset'] = array(
  '#type' => 'fieldset',
  '#title' => t('categories group'),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,

When refreshing the page the fieldset is not collapsed although it has the collapsed class. When clicking on the fieldset for the first time, the class is removed, while clicking for the second time the class is added and the fieldset is collapsed.

How do I force a fieldset to be collapsed by default?

  • 1
    According to the docs this should work unless you have interference from a module or custom theme overriding the behavior. – Kevin Dec 15 '18 at 16:43
  • Using '#collapsed' => TRUE is what Drupal core does. If you have any issue, that most probably depends from the used theme, or there is JavaScript code that uncollapse the fieldset. – kiamlaluno Dec 16 '18 at 10:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.