Hey guys
I really need this module for a job I am currently doing so I started converting some of the sql but got stuck.
I really don't know how to deal with the %s in the case
Here is my progress any help would be great.
model/hs_taxonomy.module
<?php
function hs_taxonomy_term_count_nodes($tid, $type = 0) {
static $count;
$term = taxonomy_term_load($tid);
$tree = _hs_taxonomy_hierarchical_select_get_tree($term->vid, $tid);
$tids = array($tid);
foreach ($tree as $descendant) {
$tids[] = $descendant->tid;
}
if (!isset($count[$type][$tid])) {
if (is_numeric($type)) {
// TODO Please convert this statement to the D7 database API syntax.
//Drupal 6
// $count[$type][$tid] = db_query(
// db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND t.tid IN (%s)"),
// implode(',', $tids))->fetchField();
// Drupal 7
$query = db_select('taxonomy_index' ,'t');
$query->fields('t');
$query->join('node', 'n', 't.nid = n.nid');
$query->distinct();
$query->addExpression('COUNT(n.nid)', 'count');
$query->condition('n.status', '1');
// ->condition('t.tid', array($tid), 'IN');
$result = $query->execute();
//print_r($query->__toString());
// print '<pre>';
foreach($result as $row){
//print_r($row);
$count[$type][$tid] = $row->tid;
}
// print '</pre>';
}
else {
// TODO Please convert this statement to the D7 database API syntax.
//Drupal 6
//$count[$type][$tid] = db_query(db_rewrite_sql("SELECT COUNT(DISTINCT(n.nid)) AS count FROM {taxonomy_term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' AND t.tid IN (%s)"), $type, implode(',', $tids))->fetchField();
// Drupal 7
$query = db_select('taxonomy_index' ,'t');
$query->fields('t');
$query->join('node', 'n', 't.nid = n.nid');
$query->distinct();
$query->addExpression('COUNT(n.nid)', 'count');
$query->condition('n.status', '1');
// ->condition('n.type', '%s');
$result = $query->execute();
//print_r($query->__toString());
// print '<pre>';
foreach($result as $row){
//print_r($row);
$count[$type][$tid] = $row->tid;
}
// print '</pre>';
}
}
return $count[$type][$tid];
}
Comments
Comment #1
lahode commentedHi,
I would rather write it:
$query = db_select('taxonomy_index' ,'t');
$query->join('node', 'n', 't.nid = n.nid');
$query->addExpression('COUNT(DISTINCT(n.nid))', 'count');
$query->condition('n.status', 1);
$query->condition('n.type', $type);
$query->condition('t.tid', $tids, 'IN');
$result = $query->execute();
Didn't test it, hope it helps. If ever you have some spare time to waist, have a look to "add term" option, I would be motivate to help this function to work on D7.
Comment #2
garethhallnz commentedHey
Here is the updated hs_taxonomy.module file with some of the sql updated to D7. Sorry guys I still have no idea how to use git from terminal (I use a gui called git-tower) and I don't really get how to make a patch.
Comment #3
wim leersIt's very simple:
That's all.
Comment #4
wim leersThis is actually a duplicate of #1068462: HS Taxonomy: "entity count" support — sponsorship needed.