function _term_level_get_groups in Term Level Field 7
Returns list of terms, group by their parents or voc.
Terms have an row_weight value set, used for ordering the table rows, correct ordering is important when rebuilding the form element:
- default_values are on top (within default_values order by term order)
- added_terms are always on the bottom (weight 9999)
- the rest is in between (according to term order)
Return value
An array containing following entries:
- 'parents': Each entry represents its own term level table group.
- 'terms': The terms that should be listed in the table, keyed by the table parent.
- 'tag_cloud': An array keyed by the table parent that either contains the entry 'all' with all tag cloud terms, or 'groups' with an additional group level for taxonomies with 3 hierarchical levels.
1 call to _term_level_get_groups()
- term_level_field_widget_form in ./
term_level.module - Implements hook_field_widget_form().
File
- ./
term_level.module, line 410 - Field type for referencing terms with a level to an entity.
Code
function _term_level_get_groups($vid, $term_entries_limit_per_group, $default_values = array(), $added_term = 0) {
$groups = array();
$groups['terms'] = array();
$groups['parents'] = array();
$groups['tag_cloud'] = array();
$tree = taxonomy_get_tree($vid, 0, TERM_LEVEL_VOC_MAX_DEPTH + 1);
// Check how many levels the vocabulary has. We currently only support
// vocabularies that have 3 (root, 1, 2) levels or less.
$max_depth = 0;
foreach ($tree as $term) {
if ($term->depth > $max_depth) {
$max_depth = $term->depth;
if ($max_depth == TERM_LEVEL_VOC_MAX_DEPTH) {
break;
}
}
}
// Flat vocabulary, use the voc name for the table group.
if ($max_depth == 0) {
$voc = taxonomy_vocabulary_load($vid);
$groups['parents'][0] = $voc->name;
}
$term_entries = 0;
foreach ($tree as $term) {
// Create the groups (only for hierarchical vocs).
if ($max_depth != 0 && $term->depth == 0) {
$groups['parents'][$term->tid] = $term->name;
$term_entries = 0;
}
else {
if ($max_depth != 1 && $term->depth == 1) {
$top_parent = reset($term->parents);
$groups['level_2'][$term->tid] = array(
'term' => $term,
'parent' => $top_parent,
);
$groups['tag_cloud'][$top_parent]['group_terms'][$term->tid] = $term;
}
elseif ($term->depth == $max_depth) {
// Check if the term should be listed in the table:
// - either all terms are shown in table
// - or the limit isn't reached yet
// - or the current term is in the default values and doesn't have the
// level 'none'
// - or the current term has been added from the tag cloud
if ($term_entries_limit_per_group == FIELD_CARDINALITY_UNLIMITED || $term_entries < $term_entries_limit_per_group || in_array($term->tid, array_keys($default_values)) || $term->tid === $added_term) {
if (in_array($term->tid, array_keys($default_values))) {
$term->level = $default_values[$term->tid];
$term->row_weight = -1;
if ($term->level == 'none') {
_term_level_groups_add_to_tag_clould($groups, $term);
}
}
if ($term->tid === $added_term) {
$term->level = NULL;
// List the new term at the bottom of the table.
$term->row_weight = 9999;
}
if (!isset($term->row_weight)) {
$term->row_weight = $term_entries;
}
_term_level_groups_add_to_table($groups, $term);
$term_entries++;
}
else {
_term_level_groups_add_to_tag_clould($groups, $term);
}
}
}
}
return $groups;
}