You are here

function select_with_style_assemble_optgroups_options_and_attributes in Select with Style 7

Returns a linear list of attributes to go with an optgroup list of options.

Parameters

$terms: Terms belonging to the vocabulary in question.

$prefix: The hierarchy depth prefix to be prepended to each child term label.

$term_transform: The term transform function, e.g., country-to-code, if desired.

Return value

A linear array of option attributes with one key for each key in the options array. The options array will reflect the taxonomy tree, flattened to a depth of one.

1 call to select_with_style_assemble_optgroups_options_and_attributes()
_select_with_style_options_and_attributes_lists in select_with_style/select_with_style.module
Build the list option attirbutes in tandem with the option values.

File

select_with_style/select_with_style.module, line 311
select_with_style.module

Code

function select_with_style_assemble_optgroups_options_and_attributes($terms, $prefix = NULL, $term_transform_callback = NULL) {
  $options = array();
  $option_attributes = array();

  // This is here to detect items that are not in optgroups, i.e. orphaned
  // children that have no children. We don't want them to be treated like
  // parents, as they ought to be selectable, not labels.
  $term_tree = array();
  select_with_style_build_term_tree($terms, $term_tree);
  select_with_style_flag_child_terms($terms, $term_tree);
  $translate = module_exists('i18n_taxonomy');
  foreach ($terms as $term) {
    $has_parent = !empty($term->parents[0]);
    $has_children = !$term->is_leaf;
    if (!$has_parent || $has_children) {

      // Parent, sets the CSS group for itself and its children.
      $css_group = select_with_style_term_to_class($term->name, $term_transform_callback);
    }

    // else keep the $css_group previously set
    if ($translate) {
      $term->name = i18n_taxonomy_term_name($term);
    }
    if (!$has_parent && $has_children) {

      // As this is a "root" parent, no $option entry is created.
      $parent_name = $term->name;
      $first_class = 'option-parent';
    }
    elseif ($has_parent) {

      // Has parent. Keep parent css_group
      $options[$parent_name][$term->tid] = str_repeat($prefix, $term->depth) . $term->name;
      $first_class = 'option-child';
    }
    else {

      // No parent and no children.
      $options[$term->tid] = str_repeat($prefix, $term->depth) . $term->name;
      $first_class = 'option-child';
    }
    $option_attributes[!$has_parent && $has_children ? $term->name : $term->tid] = array(
      'class' => (empty($css_group) ? $first_class : "{$first_class} group-{$css_group}") . ' tid-' . $term->tid . ' depth-' . $term->depth,
    );
  }
  return array(
    $options,
    $option_attributes,
  );
}