View source
<?php
define('TAXONOMY_BLOCK_PERM_ACCESS', 'access taxonomy_block');
define('TAXONOMY_BLOCK_PERM_ADMIN', 'administer taxonomy_block');
function taxonomy_block_help($section) {
switch ($section) {
case 'admin/help#taxonomy_block':
return '<p>' . t('Displays taxonomy hierarchy in a block.') . '</p>';
case 'admin/modules#description':
return 'Displays taxonomy hierarchy in a block.';
}
}
function taxonomy_block_permission() {
return array(
TAXONOMY_BLOCK_PERM_ACCESS => array(
'title' => t('Access taxonomy block'),
'description' => t('Read access for taxonomy block.'),
),
TAXONOMY_BLOCK_PERM_ADMIN => array(
'title' => t('Administer taxonomy block'),
'description' => t('Administration access for taxonomy block.'),
),
);
}
function taxonomy_block_menu() {
$items = array();
$items['admin/config/taxonomy_block'] = array(
'title' => 'Taxonomy Block',
'description' => 'Displays taxonomy hierarchy',
'access arguments' => array(
TAXONOMY_BLOCK_PERM_ADMIN,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'taxonomy_block_admin_settings',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'taxonomy_block.settings.inc',
);
return $items;
}
function taxonomy_block_block_info() {
$blocks['taxonomy_block'] = array(
'info' => t('Taxonomy Block'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function taxonomy_block_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'taxonomy_block':
if ($vid = variable_get('taxonomy_block_vid', 0)) {
$block['subject'] = taxonomy_vocabulary_load($vid)->name;
$block['content'] = taxonomy_block_generate_list(taxonomy_block_build_taxonomy_tree($vid, 0), (bool) variable_get('taxonomy_block_node_count', 0));
}
break;
}
return $block;
}
function taxonomy_block_build_taxonomy_tree($vid, $parent = 0, $max_depth = NULL) {
static $results;
$args = func_get_args();
$hash = md5(implode('-', $args));
if (!isset($results[$hash])) {
$terms = array();
$data = taxonomy_get_tree($vid, $parent, $max_depth);
foreach ($data as $term) {
$terms[$term->tid] = $term;
}
$terms = taxonomy_block_nest_taxonomy_terms($terms, $parent);
$results[$hash] = $terms;
}
return $results[$hash];
}
function taxonomy_block_nest_taxonomy_terms($terms, $parent) {
foreach ($terms as $term) {
$parent_tid = isset($term->parents[0]) ? $term->parents[0] : 0;
if ($parent_tid) {
if (isset($terms[$parent_tid])) {
$terms[$parent_tid]->children[$term->tid] = $term;
unset($terms[$term->tid]);
}
elseif ($parent_tid != $parent) {
taxonomy_block_nest_taxonomy_terms_child($terms, $parent_tid, $term);
unset($terms[$term->tid]);
}
}
}
return $terms;
}
function taxonomy_block_nest_taxonomy_terms_child(&$terms, $parent_tid, $child) {
foreach ($terms as &$term) {
if ($term->tid == $parent_tid) {
$term->children[$child->tid] = $child;
break;
}
elseif (!empty($term->children)) {
taxonomy_block_nest_taxonomy_terms_child($term->children, $parent_tid, $child);
}
}
}
function taxonomy_block_generate_list($hierarchy, $show_counts = FALSE) {
$items = array();
foreach ($hierarchy as $term) {
$title = $term->name;
if ($show_counts) {
$title .= t(' (@count)', array(
'@count' => taxonomy_block_count_nodes_term($term->tid),
));
}
$item = l($title, 'taxonomy/term/' . $term->tid);
if (!empty($term->children)) {
$item .= taxonomy_block_generate_list($term->children, $show_counts);
}
$items[] = $item;
}
return theme('item_list', array(
'items' => $items,
));
}
function taxonomy_block_count_nodes_term($tid) {
return db_select('taxonomy_index', 'ti')
->condition('tid', (int) $tid)
->countQuery()
->execute()
->fetchField();
}