Question

I'm creating a view that lists the top 10 most popular taxonomy terms (tags). Currently I have the view returning all the terms and I can limit the view to 10 but I can't work out how to order the terms by popularity (i.e. number of times used across all nodes).

Anyone have experience of this?

Was it helpful?

Solution 3

In the end I created my own custom module to get the terms from the database and group/sort them.

Please note that I've modified the code below slightly for posting and I've not tested the modified version. It's also worth noting that it was written for a site using PostgreSQL, but it should work with MySQL.

/**
  * Implements hook_block_info().
  */
function MYMODULE_block_info() {

  $blocks['poptags'] = array(
    'info' => t('Most Popular Tags'),
    'cache' => DRUPAL_NO_CACHE
  );

  return $blocks;
}

/**
  * Implements hook_block_view().
  */
function MYMODULE_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'poptags':
      $block['subject'] = t('Most Popular Tags');
      $block['content'] = _MYMODULE_popular_terms();
      break;
  }
  return $block;
}

function _MYMODULE_popular_terms() {

    $vocabId = 1;

    $links = array();
    $results = db_query_range('SELECT taxonomy_term_data.tid, taxonomy_term_data.name, count(taxonomy_term_data.tid) AS times_used FROM taxonomy_term_data INNER JOIN taxonomy_index ON taxonomy_term_data.tid = taxonomy_index.tid WHERE taxonomy_term_data.vid = :vid GROUP BY taxonomy_term_data.tid, taxonomy_term_data.name ORDER BY times_used DESC', 0, 10, array(':vid' => $vocabId));
    foreach ($results as $term) {
        $links['term-link-' . db_escape_field($term->name)] = array('title' => $term->name, 'href' => drupal_get_path_alias('taxonomy/term/' . $term->tid));
    }

    return theme('links', array('links' => $links, 'attributes' => array('class' => 'term-links')));
}

Don't forget to change MYMODULE for the name of your module. Lastly change the $vocabId = 1 line in the _MYMODULE_popular_terms function to the vid (vocabulary id) of the vocabulary that you want to list the terms of.

Note this is for Drupal 7 only, though it would not take much to port it to Drupal 6.

OTHER TIPS

  1. Create a new view
  2. limit to taxonomies "show taxonomies of term all"
  3. Add a relationship "Taxonomy term: Content with term"
  4. Use aggregation
  5. Add field "Content: Title"
  6. Aggregation type "Count"
  7. Change label to something like "number of times used"
  8. Add Sort criteria "Content: Title"
  9. Aggregation type "Count"
  10. Sort to whatever you like

it should look like this:

the view the result

Views 3 has a (very beta) "group by" feature; you should be able to use this and order a count field.

I wouldn't guarantee it works, but it is probably worth a try.

You could pull the data from tagadelic.

$output = '';
$vids = array(1, 2, 3, 4); #Taxonomy vocabulary-ids you want to be included.
$top_tags = tagadelic_get_weighted_tags($vids, 6, 10);
foreach ($terms as $term) {
  $weight = $term->weight;
  $output .= l($term->name, drupal_get_path_alias('taxonomy/term/' . $term->tid), array(
    'attributes' => array(
      'class' => array("tagadelic", "level$weight"),
      'rel' => 'tag',
      'title'  => $term->description,
      )
    )
  ) . " \n";
}

return $output;

The only downside is that tagadelic adds some minor overhead for calcuating the "weight", usually to present the tag sizes, which you will not not use.

The upside is that you get caching for free.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top