You are here

function content_taxonomy_allowed_values_opt_groups in Content Taxonomy 7

Helper function for generating opt groups.

Similar to content_taxonomy_allowed_values(), but unfortunately we cannot directly change content_taxonomy_allowed_values() as it only has the field variable and opt groups are settings on the instance level. Still, this is not a big performance issue, as taxonomy_get_tree statically caches some data.

1 call to content_taxonomy_allowed_values_opt_groups()
content_taxonomy_field_widget_form_alter in ./content_taxonomy.module
Implements hook_field_widget_form_alter().

File

./content_taxonomy.module, line 166

Code

function content_taxonomy_allowed_values_opt_groups($field) {
  $options = array();
  foreach ($field['settings']['allowed_values'] as $tree) {
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
      $terms = content_taxonomy_get_terms($field, $vocabulary, 0, 2);
      if (!empty($terms) && is_array($terms)) {
        $current_group_term = NULL;
        foreach ($terms as $term) {
          if ($term->depth == 0) {
            $current_group_term = $term;
          }
          elseif ($term->depth == 1 && !is_null($current_group_term)) {
            $options[$current_group_term->name][$term->tid] = $term->name;
          }
        }
      }
    }
  }
  return $options;
}