Last active
August 29, 2015 14:24
-
-
Save nguyenthanhxuan/aa5116b77d5ff53531a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param tid | |
* Term ID | |
* @param child_count | |
* TRUE - Also count all nodes in child terms (if they exists) - Default | |
* FALSE - Count only nodes related to Term ID | |
*/ | |
function term_nc($tid, $child_count = TRUE) { | |
$tids = array($tid); | |
if ($child_count) { | |
$tids = array_merge($tids, term_get_children_ids($tid)); | |
} | |
global $language; | |
$langs = array($language->language); | |
$langs[] = 'und'; | |
$query = db_select('taxonomy_index', 't'); | |
$query->condition('tid', $tids, 'IN'); | |
$query->join('node', 'n', 't.nid = n.nid'); | |
$query->condition('n.status', 1, '='); | |
$query->condition('n.language', $langs, 'IN'); | |
$count = $query->countQuery()->execute()->fetchField(); | |
return $count; | |
} | |
/** | |
* Retrieve ids of term children . | |
* | |
* @param $tid | |
* The term's ID. | |
* @param $tids | |
* An array where ids of term children will be added | |
*/ | |
function term_get_children_ids($tid) { | |
$children = taxonomy_get_children($tid); | |
$tids=array(); | |
if (!empty($children)) { | |
foreach($children as $child) { | |
$tids[] = $child->tid; | |
$tids = array_merge($tids, term_get_children_ids($child->tid)); | |
} | |
} | |
return $tids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment