8

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?

9
  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

  • I think this work correctly, vote Up – Yusef Mohamadi Jan 13 '16 at 17:23
  • Still works with Drupal 8! – Insasse May 17 at 13:33
  • that awkward moment when you get a notification from SE and you read your own answer from 7 years ago and you say "wow, what am I saying?" – saadlulu May 17 at 16:19
0

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.

  • I tried and tried and could not get it working. :-( – Camsoft Apr 18 '11 at 15:58
  • Sorry to hear that, I have had it working with something in the past, but was limited by it not using CCK fields. It is getting there but not yet. – Jeremy French Apr 18 '11 at 16:22
  • The "very beta" should be enough to not consider this as an answer :) -1, for the fact that the answer points to a solution that is (still) known to have flaws, is unstable and will more certainly not work then work. – berkes Apr 19 '11 at 10:26
  • it's still helpful for me to know Views 3 should be able to handle it soon. Thanks for the clarifications! – uwe Jul 1 '11 at 21:14
0

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.

0

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.

  • I think my solution probably has the least overhead, as it's just a simple query. Also the question was regarding Drupal7 and Tagadelic does not have a d7 release. – Camsoft Apr 19 '11 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.