The checkbox/radio widget provides an option for adding a hyphen in front of second-level items, but this doesn't allow for much theming. My request is that this module implement the same option provided by core taxonomy + betterselect, and add the following classes:

"checkbox-depth-x" -css for a checkbox with a depth of x
"radio-depth-x" -ditto for radios
"has-children" -css for any parent term

Example: a 3 level hierarchy with one term per level would look like this (in pseudo-code, anyway):

<div class = "checkbox-depth-0 has-children">
First Term 
</div>
<div class = "checkbox-depth-1 has-children">
Second Term (child of 1st)
</div>
<div class = "checkbox-depth-2">
Third Term (child of 2nd)
</div>

Thanks for your consideration.

CommentFileSizeAuthor
#4 content_taxonomy.module.patch393 bytesMac Clemmens
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vodoleq’s picture

Subscribe

YK85’s picture

subscribing

Mac Clemmens’s picture

The current limitation stems from the function around line 265 of content_taxonomy.module


    foreach ($tree as $term) {
      _content_taxonomy_localize_term($term);
      if ($field['widget']['show_depth']) {
        $value = str_repeat(' - ', $term->depth) . $term->name;
      }
      else {
        $value = $term->name;
      }
      //do a check_plain except for selects because form api does that
      $options[$term->tid] = ($field['widget']['type'] == 'content_taxonomy_select') ? $value : check_plain($value);
    }

Changing it to something like this would enable us to add a span onto the label, but not the entire object.


    foreach ($tree as $term) {
      _content_taxonomy_localize_term($term);
      
      //do a check_plain except for selects because form api does that
      $value = ($field['widget']['type'] == 'content_taxonomy_select') ? $term->name : check_plain($term->name);
      
      if ($field['widget']['show_depth']) {
        $value = "<span class='checkbox-depth-" . $term->depth ."'>" . $value . "</span>"; 
      }
      $options[$term->tid] = $value;
      
    }

When I get more time I'll take a look at the theming functions to figure out where an override would be, but maybe this little snippet will help point someone else in the right direction in the meantime.

Mac Clemmens’s picture

FileSize
393 bytes

OK -- found a much simpler way to deal with this.

This patch gets content_taxonomy select lists to work with betterselect. It updates content_taxonomy to use the same indent formatter that the taxonomy module uses by default. This allows betterselect to properly handle the field.

Currently, taxonomy_content uses " - " to indent the terms, however, taxonomy just uses "-". The betterselect code counts on a single indent with no whitespace:


/* EXCERPT FROM BETTERSELECT.MODULE, LINE 149 */

    // Add classes to items that indicate depth, for taxonomy, useful for
    // styling parent terms differently.
    if (variable_get('betterselect_add_depth_classes', FALSE)) {
      $prev = NULL;
      $prev_depth = 0;
      foreach (element_children($element) as $key) {
        $title = $element[$key]['#title'];
        $trim_title = ltrim($title, '-');
        $depth = strlen($title) - strlen($trim_title);
        $extra_class = 'checkbox-depth-' . $depth;

        $element[$key]['#prefix'] = '<div class="' . $extra_class . '">';
        $element[$key]['#suffix'] = '</div>';

        // Put a class on each element that contains children. We do this
        // retroactively, in other words, we operate on the previous element.
        if ($prev != NULL) {
          if ($depth > $prev_depth) {
            // Gone down a level, so previous must get 'has-children' class
            $alt_classes = 'checkbox-depth-' . $prev_depth . ' has-children';
            $prev['#prefix'] = '<div class="' . $alt_classes . '">';
            $prev['#suffix'] = '</div>';
          }
        }
        $prev = &$element[$key];
        $prev_depth = $depth;
      }
    }
colemanw’s picture

Great work!

Andrew Gorokhovets’s picture

Unfortunately, it (#3, #4) is does not work for me.


<div class="form-item">
 <label>MY LABEL: </label>
 <div class="form-checkboxes">

<div id="edit-field-metro-value-3431-wrapper" class="form-item">
 <label for="edit-field-metro-value-3431" class="option">
  <input type="checkbox" class="form-checkbox" value="3431" id="edit-field-metro-value-3431" name="field_metro[value][3431]"> 1
</label>
</div>

<div id="edit-field-metro-value-3432-wrapper" class="form-item">
 <label for="edit-field-metro-value-3432" class="option">
<input type="checkbox" class="form-checkbox" value="3432" id="edit-field-metro-value-3432" name="field_metro[value][3432]"> 2
</label>
</div>

</div>
</div>