function taxonomy_menu_block_get_nodes in Taxonomy menu block 7
Get the number of nodes attached to the terms of a vocabulary.
We try to keep this as performant as possible by doing one query for all the terms in a vocabulary, which we will then cache.
2 calls to taxonomy_menu_block_get_nodes()
- taxonomy_menu_block_hide_empty in ./
taxonomy_menu_block.module - Delete the terms with no nodes attached to them.
- taxonomy_menu_block_nodes in ./
taxonomy_menu_block.module - Attach nr. of nodes to each term, plus the optional node options.
File
- ./
taxonomy_menu_block.module, line 681 - Taxonomy Menu Block module allows you to make menu blocks out of your taxonomies in a very performant way.
Code
function taxonomy_menu_block_get_nodes($tids, $vid, $ctype) {
$nodes = array();
if (!empty($ctype)) {
$ctypestring = implode('-', $ctype);
}
// First check if our vocabulary is set to localize. If this is the case
// nodes in different languages will be attached to the same tid.
if (module_exists('i18n_taxonomy') && i18n_taxonomy_vocabulary_mode($vid) == 1) {
global $language;
$lang = $language->language;
if (!empty($ctype)) {
$cache_identifier = 'taxonomy_menu_block-' . $vid . '-nodes-' . $lang . '-' . $ctypestring;
}
else {
$cache_identifier = 'taxonomy_menu_block-' . $vid . '-nodes-' . $lang;
}
// First check if we have the result in cache already.
$cache = cache_get($cache_identifier);
if ($cache) {
$nodes = $cache->data;
}
else {
// Query for nodes attached to every term of the tree.
// Later we'll sift out the node counts we need from this global result.
$query = db_select('taxonomy_index', 'ti');
$query
->fields('ti', array(
'tid',
));
$query
->join('node', 'n', 'ti.nid = n.nid');
$query
->join('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid');
$query
->condition('ttd.vid', $vid, '=');
$query
->condition('n.language', $lang, '=');
$query
->groupBy('ti.tid');
if (!empty($ctype)) {
$or = db_or();
foreach ($ctype as $type) {
$or
->condition('n.type', $type, '=');
}
$query
->condition($or);
}
$query
->addExpression('COUNT(ti.tid)', 'count');
$nodes = $query
->execute()
->fetchAllKeyed();
$cache = cache_set($cache_identifier, $nodes);
}
}
else {
if (!empty($ctype)) {
$cache_identifier = 'taxonomy_menu_block-' . $vid . '-nodes' . '-' . $ctypestring;
}
else {
$cache_identifier = 'taxonomy_menu_block-' . $vid . '-nodes';
}
$cache = cache_get($cache_identifier);
if ($cache) {
$nodes = $cache->data;
}
else {
// Here we don't need to take language in account. Every term has its own
// tid.
$query = db_select('taxonomy_index', 'ti');
$query
->fields('ti', array(
'tid',
));
$query
->join('taxonomy_term_data', 'ttd', 'ti.tid = ttd.tid');
$query
->condition('ttd.vid', $vid, '=');
$query
->groupBy('ti.tid');
if (!empty($ctype)) {
$query
->join('node', 'n', 'ti.nid = n.nid');
$or = db_or();
foreach ($ctype as $type) {
$or
->condition('n.type', $type, '=');
}
$query
->condition($or);
}
$query
->addExpression('COUNT(ti.tid)', 'count');
$nodes = $query
->execute()
->fetchAllKeyed();
$cache = cache_set($cache_identifier, $nodes);
}
}
// Find the same values of our two arrays
$tids = array_flip($tids);
$nodes = array_intersect_key($nodes, $tids);
return $nodes;
}