1

I want to have collapsible fieldsets in my Drupal 7 website in add content. I added this in html.tpl.php of my theme:

<?php 
print $styles;
drupal_add_js('misc/form.js');
    drupal_add_js('misc/collapse.js');

?>

But nothing happend to my fieldsets. Here's a sample code of fieldset I have used:

<fieldset id="fieldset-id" class="collapsible">
  <legend><span class="fieldset-legend">Fieldset title</span></legend>
  <div class="fieldset-wrapper">
  <div class="fieldset-description">Fieldset description</div>
     Fieldset content
  </div>
</fieldset>

What is the problem?

1
  • do you have any js errors ? - check console
    – Victor
    Oct 30 '13 at 20:52
1

Your theme might include jQuery 1.9. With this, I observed in Drupal 7.34 that fieldsets would not work (always shown collapsed, unclickable). Also CKEditor would not load (using the ckeditor module) and there might be other JavaScript related misbehavior.

To solve this, use jQuery 1.8 instead. I recommend the module jquery_update to handle jQuery versions in Drupal.

1

I think that the libraries are others.

This example works for me:

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

Read more about this at Fieldsets in D7, collapsed and collapsible not working.

0

You should do that using the Form API.

Here's an example implementation through a custom module:

/**
 * Sample form.
 */
function MYMODULE_sample_form() {
  $form['sample'] = array(
    '#type' => 'fieldset',
    '#title' => t('Sample fieldset'),
    '#description' => t('Sample fieldset description'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['sample']['sample_textfield'] = array(
    '#title' => t('Text field'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Submit the form'));

  return $form;
}

To display the form above, use the following code:

<?php
$form = drupal_get_form('MYMODULE_sample_form');
print drupal_render($form);
?>

However, it's not quite clear where exactly you want to display this. Sounds as if you want to alter some node creation form; if that is the case, post a comment and I'll update the example code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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