Опубликовано пт, 03/18/2016 - 13:43 пользователем Armen
Функция для получения терминов таксономии в виде иерархии:
function taxonomy_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) { if (is_int($terms)) { $terms = taxonomy_get_tree($terms); } foreach($terms as $term) { foreach($term->parents as $term_parent) { if ($term_parent == $parent) { $return[$term->tid] = $term; } else { $parents_index[$term_parent][$term->tid] = $term; } } } foreach($return as &$term) { if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) { $term->children = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1); } } return $return; }
Функция для вывода терминов в виде списка:
function output_taxonomy_nested_tree($tree) { if (count($tree)) { $output = '<ul class="taxonomy-tree">'; foreach ($tree as $term) { $output .= '<li class="taxonomy-term">'; $output .= $term->name; if (!empty($term->children)) { $output .= output_taxonomy_nested_tree($term->children); } $output .= '</li>'; } $output .= '</ul>'; } return $output; }
Простая функция для получения вложенного массива из taxonomy_get_tree().
function simple_taxonomy_tree($vid, $parent = 0) { $items = array(); $terms = taxonomy_get_tree($vid); foreach ($terms as $term) { if (in_array($parent, $term->parents)) { $items[$term->tid] = array( 'data' => $term->name, 'tid' => $term->tid, 'children' => _mymodule_taxonomy_tree($terms, $term->tid), ); } } return $items; }