For some reason I cannot get the theme_fieldset function to make me a collapsible or collapsed fieldset.

This code:

$page['xyz'] = theme('fieldset', array('element' => array('#title' => 'XYZ', '#value' => $xyz, '#collapsed' => TRUE, '#collapsible' => TRUE)));

should as far as I can see from the docs give me a collapsible fieldset starting out collapsed.
http://api.drupal.org/api/drupal/includes--form.inc/function/theme_field...

It gives me a fieldset, but neither collapsed nor collapsible.

Wondering why...

I can't find any examples online, and honestly; looking in the code I can't see where the theme function actually checks for these parameters and creates the fieldset appropriately. Looking through my D7 installation, I find one reference to theme_fieldset or theme('fieldset' ...), apart from in my own code, and that's in form.inc where the function is declared.

Am I theming the fieldset the completely wrong way?

Martin

Comments

vasi1186’s picture

Hi,

you also have to include the collapse library for d7. You can do it like this:

drupal_add_library('system', 'drupal.collapse');

Vasi.

vertikal.dk’s picture

Vasi,

Thanks for the tip! I didn't realize that... but unfortunately that doesn't quite do it.

The behavior of the fieldset remains the same: no behavior. But I did get the small arrows that indicates collapsible sections, so something has happened.

But going into a little js debugging I notice that I get this error when the library is included:

"Error: uncaught exception: Syntax error, unrecognized expression:"

when the page loads. It seems to come from jQuery. FireFox has no further information while IE tells me that it stems from jquery.js itself. I have no idea what causes this.

I'll dig into this and see where it leads me.

Thanks for your help.

Martin

xandeadx’s picture

me too
subscr

dj1999’s picture

use '#collapsible' and '#collapsed' parameters the theme_fieldset() function.

I was wrote a patch, it is failed in test but works fine for me.
Thread is here: http://drupal.org/node/1099132
If somebody knows what is the problem with this patch please let me know too.

Regards,
Joe

CzarnyZajaczek’s picture

This code works for me

$renderable['fieldset'] = array(
  '#type' => 'fieldset',
  '#title' => t('Title'),
  'example content' => array (
    '#theme' => 'item_list',
    '#items' => array(
      t('Example item')
    )
  ),
  '#attributes' => array (
    'class' => array(
      'collapsible', 'collapsed'
    )
  )
);

drupal_render($renderable);

If still doesn't work, you may try

drupal_add_library('system', 'drupal.collapse');

or


drupal_add_js('misc/form.js'); // don't know if its really required to collapsible fieldsets to work
drupal_add_js('misc/collapse.js'); 
alexweber’s picture

Thanks, worked for me!

pol’s picture

Actually, the best way to do this is:

      $fieldset = array(
        '#type' => 'fieldset',
        '#attached' => array(
          'library' => array(
            array('system', 'drupal.collapse'),
          ),
        ),
        '#attributes' => array(
          'class' => array('collapsible', 'collapsed')
          ),
        ),
     );
jaypan’s picture

To tie into the API I'd do it like this:

$fieldset = array(
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#attached' => array(
          'library' => array(
            array('system', 'drupal.collapse'),
          ),
        ),
     );

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

pol’s picture

Nope, this doesn't work. You must include manually the classes.

jaypan’s picture

Ok, good to know. I'm guessing that the fieldset needs to be part of a form for #collapsed and #collapsible to work.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

donquixote’s picture

Thanks for this hint!

I confirm, the form_process_fieldset() is only called if the fieldset is within a form.

So the classes and the library must be added manually if outside a form.

stavroch’s picture

in which file do i have to add this lines?

kerasai’s picture

Calling the theme() function directly or using a renderable array outside of a form does not work because it doesn't invoke Drupal's form element #process code on the fieldset.

An even cleaner way to do this is to call Drupal's form element #process on the renderable array.

$fieldset = array(
  '#type' => 'fieldset',
  '#title' => t('My fieldset'),
  ...
);
$form_state = array();
form_process_fieldset($fieldset, $form_state);

This will leave $fieldset as a renderable array with all of the proper classes set and the required libraries attached.

argiepiano’s picture

Adding: 

'#parents' => array(),

to the $fieldset array will prevent a warning about #parents not being set, thrown by form_process_field