It would be nice to have a theme where group tags are seperated by colors.

Comments

evoltech’s picture

Status: Active » Closed (won't fix)
Issue tags: +add in Drupal6 port

There is a much more elegant way to do this in drupal 6, but in drupal 5 I don't know how to do this whith out hacking some theme code (please correct me if there is a way to do this in the module).

That being said you can accomplish this by adding the following theme code to your template.php file:

/**                                                                           
 * Format a dropdown menu or scrolling selection box.                         
 *                                                                            
 * @param $element                                                            
 *   An associative array containing the properties of the element.           
 *   Properties used: title, value, options, description, extra, multiple, required  
 * @return                                                                    
 *   A themed HTML string representing the form element.                      
 *
 * It is possible to group options together; to do this, change the format of 
 * $options to an associative array in which the keys are group labels, and the
 * values are associative arrays in the normal $options format.
 */
function phptemplate_select($element) {
  $select = '';
  $size = $element['#size'] ? ' size="' . $element['#size'] . '"' : '';
  _form_set_class($element, array('form-select'));
  $multiple = isset($element['#multiple']) && $element['#multiple'];
  return theme('form_element', $element,
    '<select name="'. $element['#name'] .''.  ($multiple ? '[]' : '') .'"'.
    ($multiple ? ' multiple="multiple" ' : '') .
    drupal_attributes($element['#attributes'])
    .' id="'. $element['#id'] .'" '. $size .'>'.
    (($element['#name'] == 'comms_group_tag') ?
      phptemplate_form_select_options($element) : form_select_options($element))
    .'</select>');
}

function phptemplate_form_select_options($element, $choices = NULL) {
  static $class = 'odd';
  static $style = 'background-color: #EDF5FA;';
  if (!isset($choices)) {
    $choices = $element['#options'];                                          
  }
  // array_key_exists() accommodates the rare event where $element['#value'] is NULL.
  // isset() fails in this situation.                                         
  $value_valid = isset($element['#value']) || array_key_exists('#value', $element);
  $value_is_array = is_array($element['#value']);
  $options = '';
  foreach ($choices as $key => $choice) {
    if (is_array($choice)) {
      $options .= '<optgroup label="'. $key .'">';
      $options .= phptemplate_form_select_options($element, $choice);
      $options .= '</optgroup>';
    }
    elseif (is_object($choice)) {
      $options .= phptemplate_form_select_options($element, $choice->option);
    }
    else { 

      $key = (string)$key;
      if ($value_valid && (!$value_is_array && (string)$element['#value'] === $key || ($value_is_array && in_array($key, $element['#value'])))) {
        $selected = ' selected="selected"';
      }
      else {
        $selected = '';
      }

      // This is where we implement the custom zebra-ing.
      if (!preg_match('/^-/', $choice)) {
        $class = ($class == 'odd') ? 'even' : 'odd';
      }
      if ($class == 'odd') {
        $options .= '<option value="'. check_plain($key) .'"'. $selected 
          .'" style="'. $style .'">'. check_plain($choice) .'</option>';
      } 
      else {
        $options .= '<option value="'. check_plain($key) .'"'. $selected
          .'">'. check_plain($choice) .'</option>';
      }
    }
  }
  return $options;
}

What you see here is us overriding the the theme_select() function (declared in includes/form.inc). theme_select relies on a helper function called form_select_options() which we call unless this is for the comms_group_tag section. If this is for the comms_group_tag section then we will add in background style for the option depending on wether or not it is a new term section. Here a term section is defined as any new parent-less term (the taxonomy module gives us terms prefixed with "-" if it is a child of the previous term).