Недавно начал работать с друпалом и возникла такая задача. Решил её, написав такой сниппет.
Посоветуйте, оформить эту функцию в виде модуля или приемлемо инклудить её по мере надобности?
<?phpfunction print_vocabulary_terms()
{
$vocs = taxonomy_get_vocabularies();
foreach ($vocs as $voc)
{
$vid = $voc->vid;
print "<li><a href=\"#\">".t($voc->name)."</a>";
$terms = taxonomy_get_tree($vid);
print "<ul>";
foreach ( $terms as $term ) {
$tcount = taxonomy_term_count_nodes($term->tid);
$children_terms = taxonomy_get_children($term->tid);
if (($term->depth == 0) & ($tcount != 0) ) {
print "<li>";
print l($term->name." (".$tcount.")",'taxonomy/term/'
.$term->tid, array('title' => $tcount." posts in "
.$term->name));
if ($children_terms) {
print "<ul>";
foreach ( $children_terms as $children_term ) {
$t_children_count =
taxonomy_term_count_nodes($children_term->tid);
print "<li>";
print l($children_term->name." (".$t_children_count.")", 'taxonomy/term/'.$children_term->tid, array('title' =>$t_children_count." posts in ".$children_term->name));
print "</li>";
}/* end foreach children */
print "</ul>";
}
print "</li>";
}
} /* end foreach */
print "</ul>";
print "</li>";
} /* end foreach */
}?>
Комментарии
1) Вставьте это в файл print_vocabulary.module, создайте print_vocabulary.info - заполните по аналогии с др. модулями. Не забудьте, что в модуле не нужен закрывающий "тэг" ?>
2) Создайте в sites/all/modules папку print_vocabulaty и вставьте в неё оба файла. Включите модуль в админке.
3) Где нужно, пишите
<?php if (function_exists(print_vocabulary_terms)) {
print print_vocabulary_terms();
}
else {
'Включите модуль "print_vocabulary"';print
}
?>
Только функция ваша должна что-то возвращать всё-таки
То есть все конструкции print бла-бла-бла; замените на $output .= бла-бла-бла; и в конце return $output;
Ну и вложенные циклы конечно не лучший выход