function featured_content_sort_nodes_by_vocab in Featured Content 7
Same name and namespace in other branches
- 6.2 featured_content.module \featured_content_sort_nodes_by_vocab()
- 6 featured_content.module \featured_content_sort_nodes_by_vocab()
- 7.2 featured_content.module \featured_content_sort_nodes_by_vocab()
Sort nodes based on vocabularies. The order is based on the weight of the vocabularies as well as the term of terms the node has within that vocabulary.
1 call to featured_content_sort_nodes_by_vocab()
- featured_content_block_view in ./
featured_content.module - Implements hook_block_view().
File
- ./
featured_content.module, line 1976 - Featured Content module for created related & featured content blocks.
Code
function featured_content_sort_nodes_by_vocab($nodes, $order = 'desc') {
// Only works if on a node page.
$node = _featured_content_load_node();
if (!empty($node)) {
$nids = array_keys($nodes);
$num_terms = array();
$weights = array();
// FIXME: For some reason taxonomy_index doesn't have a node vid but should.
$count_query = db_select('taxonomy_index', 'ti');
$count_query
->addExpression('COUNT(*)', 'count');
$query = db_select('taxonomy_index', 'ti');
$query
->setCountQuery($count_query);
$query
->fields('ti', array(
'nid',
'tid',
'vid',
'weight',
));
$query
->join('node', 'n', 'ti.nid = n.vid');
$query
->join('taxonomy_term_data', 'td', 'ti.tid = td.tid');
$query
->join('taxonomy_vocabulary', 'v', 'td.vid = v.vid');
$query
->join('taxonomy_index', 'ti2', 'ti.tid = ti2.tid');
$query
->join('node', 'n2', 'ti2.nid = n2.nid');
$query
->condition('n2.nid', $node->nid);
$query
->condition('ti.nid', $nids);
$query
->groupBy('ti.nid');
$query
->groupBy('v.weight');
$query
->orderBy('v.weight', $order);
$results = $query
->execute();
foreach ($results as $row) {
$num_terms[$row->vid][$row->nid] = $row->count;
$weights[$row->vid] = $row->weight;
}
asort($weights);
$totals = array();
foreach ($nids as $nid) {
$nums = array();
foreach ($weights as $vid => $weight) {
$nums[] = isset($num_terms[$vid][$nid]) ? $num_terms[$vid][$nid] : 0;
}
$totals[$nid] = implode('-', $nums);
}
natsort($totals);
$totals = array_reverse($totals, TRUE);
$tmp = array();
foreach ($totals as $nid => $total) {
$tmp[$nid] = $nodes[$nid];
}
foreach ($nodes as $nid => $node) {
if (!in_array($nid, array_keys($tmp))) {
// Tack on the rest of the nodes.
$tmp[$nid] = $node;
}
}
$nodes = $tmp;
}
return $nodes;
}