ctools Modals and Vertical Tabs ctools Modals and Vertical Tabs

Posted by mradcliffe on 2011.12.21 @ 22:49

Filed under:

The Chaos tool suite (ctools) is collection of useful methods intended to help Drupal developers create complex interfaces without duplicating too much effort.

Drupal 7 introduced a new Form API element: Vertical Tabs. When you set a parent element as this Form API type, then any child elements that are of type fieldset are displayed as vertical tabs instead of as in the traditional fieldset. Unfortunately for us developers, the documentation does not provide an example of how to use Vertical Tabs properly, but with some trial and error you can do it.

However, this new Vertical Tab Form API element doesn’t work in modal multistep wizard forms created with ctools.

If you look at the theme_vertical_tabs() method, you will notice it loads a Javascript library to do its magic.

This library won’t be loaded properly in a modal dialog. Instead you need to call that same drupal_add_library() method in your ctools form.

function blah_form($js, $step) {
 
  drupal_add_library('system', 'drupal.vertical-tabs');  
 
  // Other stuff...  The ctools_ajax_example module is a decent example of this part. 
}

and then you can use vertical tabs in your other form callbacks!

function blah_step1($form, &$form_state) {
  $form['blah'] = array(
    '#type' => 'vertical_tabs',
    '#tree' => TRUE,
  );
 
  for ($i = 0; $i < 10; $i++) {
    $form['blah'][$i] = array(
      '#type' => 'fieldset',
      '#title' => t('Blah !n', array('!n' => $i),
      '#group' => 'blah',
      '#tree' => TRUE,
    );
 
    $form['blah'][$i]['blahblah'] = array(
      '#type' => 'radios',
      '#title' => t('Blah Blah?'),
      '#options' => array(0 => t('No'), 1 => t('Yes'), 2 => t('Maybe')),
      '#default_value' => 0, 
    );
  }
 
  return $form;
}

Update 1/4/2012: This method is incomplete for Internet Explorer. Internet Explorer does not allow you to inject CSS into the DOM directly. This can be averted by using the addImport method, which only Internet Explorer supports. This is addressed in Lazy-loading CSS fails in IE issue on drupal.org so hopefully you will not need to do the following for much longer in the future.

  drupal_add_js('
    if (document.styleSheets[0].addImport) {
      document.styleSheets[0].addImport(' . DRUPAL_ROOT . '/misc/vertical_tabs.css);
    }
  ', 'inline');

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <pre> <div> <span> <b> <i> <br> <table> <tr> <td> <th> <small> <sub> <tex> <equation> <equations> <s>
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [applescript], [bash], [c], [cpp], [css], [diff], [drupal5], [drupal6], [html4], [java], [javascript], [latex], [lisp], [lua], [m68k], [make], [mysql], [objc], [pascal], [perl], [php], [python], [ruby], [sql], [xml].
  • You may insert videos with [video:URL]
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Adds typographic refinements.
  • LaTex commands embedded in text will be interpreted and rendered. Additional information can be found at DruTex Documentation Pages
    • Allowed commands include: \atop \binom \cdot \cfrac \choose \frac \int \ln \over \sum \to
      Allowed environments include: align array equation equations gather matrix split
    • Provides different environments to create rendered images (especially maths).
    • Assists automatic numbering of tex, equation, and equations environments.

    More information about formatting options