[development] Creating embedded unordered lists of checkboxes

Steve Edwards killshot91 at gmail.com
Thu Jan 14 00:54:03 UTC 2010


I have a need to create a two-level unordered list of checkboxes from a two level taxonomy vocabulary.  So for instance, if my vocabulary is 

- Level 1 term 1
-- Level 2 term 1
-- Level 2 term 2
-- Level 2 term 3
- Level 1 term 2
-- Level 2 term 4
-- Level 2 term 5
- Level 1 term 3

I want to create the following:

<ul>
 <li><input type = checkbox">Level 1 term 1</li>
 <ul>
   <li><input type = checkbox">.Level 2 term 1</li>
   <li><input type = checkbox">Level 2 term 2</li>
   <li><input type = checkbox">Level 2 term 3</li>
 </ul>
 <li><input type = checkbox">Level 1 term 2</li>
 <ul>
   <li><input type = checkbox">Level 2 term 4</li>
   <li><input type = checkbox">Level 2 term 5</li>
 <ul>
 <li><input type = checkbox">Level 1 term 3<li>
</ul>

What is the best way to do this?  Just use a checkboxes element type and create my own theme function for the form?  Modify something like theme_item_list?  Or is there a better (and easier) way that I'm missing?

What I tried doing was to create a theme function for my checkboxes element by setting the #theme property for the element to my custom theme function (and registering the function in hook_theme).  I then just made a copy of theme_checkboxes() (and theme_form_element since it's called from theme_form_checkboxes) and renamed to match hook_theme and the #theme property.  However, when doing that, none of my checkboxes are rendered at all.  On stepping through the code, I found the problem in drupal_render starting at line 2868:

  if (!isset($elements['#children'])) {
    $children = element_children($elements);
    // Render all the children that use a theme function.
    if (isset($elements['#theme']) && empty($elements['#theme_used'])) {
      $elements['#theme_used'] = TRUE;

      $previous = array();
      foreach (array('#value', '#type', '#prefix', '#suffix') as $key) {
        $previous[$key] = isset($elements[$key]) ? $elements[$key] : NULL;
      }
      // If we rendered a single element, then we will skip the renderer.
      if (empty($children)) {
        $elements['#printed'] = TRUE;
      }
      else {
        $elements['#value'] = '';
      }
      $elements['#type'] = 'markup';

      unset($elements['#prefix'], $elements['#suffix']);
      $content = theme($elements['#theme'], $elements);

      foreach (array('#value', '#type', '#prefix', '#suffix') as $key) {
        $elements[$key] = isset($previous[$key]) ? $previous[$key] : NULL;
      }
    }
    // Render each of the children using drupal_render and concatenate them.
    if (!isset($content) || $content === '') {
      foreach ($children as $key) {
        $content .= drupal_render($elements[$key]);
      }
    }
  }

So basically, because I have #theme set for the checkboxes field, it skips the rendering of the individual checkbox elements.  To me, it makes sense that I override the theme function for the checkboxes type since that's the element type, but that doesn't seem to be the case.  So what do I need to do to be able to simply theme my checkboxes element?

Thanks.

Steve



More information about the development mailing list