You are here

function featured_content_sort_nodes_by_terms in Featured Content 7

Same name and namespace in other branches
  1. 6.2 featured_content.module \featured_content_sort_nodes_by_terms()
  2. 6 featured_content.module \featured_content_sort_nodes_by_terms()
  3. 7.2 featured_content.module \featured_content_sort_nodes_by_terms()

Sort nodes based on terms.

1 call to featured_content_sort_nodes_by_terms()
featured_content_block_view in ./featured_content.module
Implements hook_block_view().

File

./featured_content.module, line 1939
Featured Content module for created related & featured content blocks.

Code

function featured_content_sort_nodes_by_terms($nodes, $order = 'desc') {

  // Only works if on a node page.
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    $tmp = array();
    $nids = array_keys($nodes);
    $count_query = db_select('taxonomy_index', 'ti');
    $count_query
      ->addExpression('COUNT(ti.tid)', 'count');
    $query = db_select('taxonomy_index', 'ti');
    $query
      ->setCountQuery($count_query);
    $query
      ->fields('ti', array(
      'nid',
    ));
    $query
      ->condition('ti.nid', $nids);
    $query
      ->join('taxonomy_index', 'ti2', 'ti.tid = ti2.tid');
    $query
      ->condition('ti2.nid', arg(1));
    $query
      ->groupBy('ti.nid');
    $query
      ->orderBy('count', $order);
    $results = $query
      ->execute();
    foreach ($results as $row) {
      $tmp[$row->nid] = $nodes[$row->nid];
    }
    foreach ($nodes as $nid => $node) {
      if (!isset($tmp[$nid])) {

        // Tack on the rest of the nodes.
        $tmp[$nid] = $node;
      }
    }
    $nodes = $tmp;
  }
  return $nodes;
}