Weighted Tag Clouds (as seen on Flickr and Technocrati) to Drupal!

fajerstarter - May 5, 2005 - 01:29

Either you like them or you don't, but they leave no one untouched. Of course I'm talkin about Weighted Tag Clouds, seen on Flickr, Technorati and del.icio.us among others.

So here it is, Drupal Weighted Tag Clouds Module!

Sidenote 1: Actually this is my first module ever built, and I'm not really a programmer (copy 'n' paste you know...), so please feel free to improve and comment on the code.
Sidenote 2: This doesn't have to be a module, just pull out the php-code and replace the variables and you can include it on any page you wan't.
Sidenote 3: Also feel free to include this in an existing module, I'd be glad.

<?php

/**
* Implementation of hook_menu().
*/
function term_popular_menu($may_cache) {
 
$items = array();

  if (
$may_cache) {
   
$items[] = array('path' => 'admin/settings/term_popular', 'title' => t('popular terms'),
     
'callback' => 'term_popular_admin',
     
'access' => user_access('administer site configuration'),
     
'type' => MENU_NORMAL_ITEM);

   
$items[] = array('path' => 'term_popular', 'title' => t('popular terms'),
     
'callback' => 'term_popular_page',
     
'access' => user_access('access content'),
     
'type' => MENU_CALLBACK);
  }

  return
$items;
}


/**
* Menu callback; present settings.
*/
function term_popular_admin() {
 
system_settings_save();

  foreach (
taxonomy_get_vocabularies() as $vocabulary) {
   
$vocabulary_fields[$vocabulary->vid] = $vocabulary->name;
  }

 
$output = form_textfield(t('Maximum number of terms'), 'max_terms', variable_get('max_terms', 200), 4, 5, t('Maximum number of terms to show on the page.'));
 
$output .= form_select(t('Vocabularies to include'), 'vocabulary_fields', variable_get('vocabulary_fields', $vocabulary_fields), $vocabulary_fields, t('Select which vocabularies to be included on the page.'), 0, 1);

  print
theme('page', system_settings_form($output));
}


/**
* Return a list of popular terms in HTML.
*/
function term_popular_list() {

 
$vocabulary_fields variable_get("vocabulary_fields", $vocabulary_fields);
 
$max = variable_get('max_terms', 200);

  if (
$vocabulary_fields != NULL  && $max != NULL) {
   
implode(', ', $vocabulary_fields);

   
$result = db_query('SELECT * FROM {term_data} WHERE vid IN (%s)', implode(', ', $vocabulary_fields));

    while (
$term = db_fetch_object($result)) {
       
$term_names[$term->tid] = $term->name;
       
$popularity[$term->tid] = taxonomy_term_count_nodes($term->tid) ;
    }

   
arsort($popularity);
   
$popularity_temp = $popularity;
   
$most_popular = (array_shift($popularity_temp));
   
natcasesort($term_names);
   
$i = 1;

    while (list(
$key, $val) = each($term_names) and $i <= $max) {
      if (
$popularity[$key] > 0) {
       
$output .= '<span style="font-size: '. round(($popularity[$key] / $most_popular * 30) + 8) . 'px">' . l( $val, 'taxonomy/term/'. $key) . '</span>' . "\n";
       
$i++;
      }
    }

    return
$output;
  }
}

/**
* Theme Popular Terms page
*/
function term_popular_page($rids = null) {
 
$output  = '<div class="term_popular">';
 
$output .= term_popular_list();
 
$output .= '</div>';
  print
theme("page", $output);
}

?>

This line is where the magic happens:

$output .= '<span style="font-size: '. round(($popularity[$key] / $most_popular * 30) + 8) . 'px">' . l( $val, 'taxonomy/term/'. $key) . '</span>' . "\n";

I'm not shure if the "math" used here is the best, go play with it! Yuo could of course also use em instead of px.

Profile

fajerstarter - May 6, 2005 - 07:27

This functionality would also be cool to have for the profile module, for example to see popular interests.

Cool

Dries - May 10, 2005 - 11:36

That would be cool, yes!

how would I use this?

larry - May 6, 2005 - 11:13

Hello fajerstarter,
Thanks for your work. My question is, how would I actually use the code above? I see no module name or way to identify this code to Drupal so it can be used. I would like to try this out and see how it compares with awtags, which is very nice work. How can I use your code as a module? Thanks again.

larry

--There are no Kangaroos in Austria--

Install instructions

fajerstarter - May 9, 2005 - 19:46

Create a folder with the name term_popular in your module directory. Then create a empty textfile with the name "term_popular.module" and paste in the code above.

Then it's the normal stuff: enable the module under admin/modules and the go to admin/settings/popular terms

thanks...

larry - May 10, 2005 - 11:39

term_popular...I had'nt tried that one. Very nice module. Works well and looks good. Thanks again.

larry

--There are no Kangaroos in Austria--

Not Working...

PiersDesigns - July 22, 2006 - 19:19

I really really want this to work, but I get a Parse Error - /modules/term_popular/term_popular.module on line 7 when I got to my admin/module page to turn it on.

HELP PLEASE!!

I think this is a great module and I would love it on my page.

please contribute

Bèr Kessels - May 9, 2005 - 21:07

We should really avoid having code and modules in the forums. It is unmaintainable.

Please apply for CVS and start by adding this module in a sandbox directory. Once you feel confident enough to release it, you can make it a proper project.

---
Next time, please consider filing a support request.

[Bèr Kessels | Drupal services www.webschuur.com]

I know

fajerstarter - May 10, 2005 - 11:24

We should really avoid having code and modules in the forums. It is unmaintainable.

I agree with you on this.

First I actually planned to release this as a module, I applied for a CVS account and got some valid feedback from Dries, informing me that there already are "tag"-modules for Drupal. This made me think a second time about this module thing:

1. I don't know much about CVS, from what I've heard (and in some extent experienced) it's not that simple, and it's also possible to mess things up.

2. I don't know much PHP

3. This is a really simple module; it may even serve better as php-code in a node.

So even if I did wrote some install instruction above (thinking back, maybe I should have avoided this), posting this code here was more because I had such a strong urge to share this code with someone :) , and maybe get some feedback... and not as an attempt to maintain a module in the forum.

Ok, why do I keep defending my ill behaviour? Because I think there are more people like me. I've seen the Theme snippets repository, but this code doesn't really fit there (or does it?). But it's a good idea, maybe this is something that can be extended? So people like me can easily share our code, and even get feedback. I'm thinking of something like this. Comments?

Feature request

fajerstarter - May 11, 2005 - 16:18

I created a feature request for the snippets repository idea mentioned above.

Which modules are tag module?

levavie - May 18, 2005 - 12:50

You wrote: "got some valid feedback from Dries, informing me that there already are "tag"-modules for Drupal."

Which modules are tag modules? Could you refer me to the original post?

awtags

fajerstarter - May 23, 2005 - 17:51

Do a search for awtags mentioned above.

Cool

conann - May 9, 2005 - 21:34

I would like to see it in action if you have a site running it.

I like it!

frjo - May 25, 2005 - 06:39

I have put a modified version of this module in my sandbox http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/frjo/.

I added some settings, help text, change font size from px to em etc. See it in action here http://xdeb.org/term_popular.

Thanks for a nice example!

Looks good

fajerstarter - May 25, 2005 - 07:01

Great work!

Great module!

SimonVlc - May 25, 2005 - 09:30

Great module fajerstarter! Thanks for your work.

Similar, but in slightly more compact form

arnabdotorg - May 25, 2005 - 15:26

As seen on http://new.arnab.org/tags :

As a table:

<?php
$sql
= "SELECT count(*) as how_many, {term_data}.name, {term_data}.tid FROM {term_node}, {term_data} WHERE {term_node}.tid = {term_data}.tid group by {term_node}.tid order by how_many DESC;";
 
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
 
$output .= "<li>".l($anode->name.'('.$anode->how_many.')', '/taxonomy/term/'.$anode->tid);
}
$output .= "</ul>";
print
$output;
$output = "";
?>

Or in Tag Soup form:

<?php
$fontmax
= -1;

$sql = "SELECT count(*) as how_many, {term_data}.name, {term_data}.tid FROM {term_node}, {term_data} WHERE {term_node}.tid = {term_data}.tid group by {term_node}.tid order by how_many DESC;";

$result = db_query($sql);

while (
$anode = db_fetch_object($result)) {

if (
$fontmax == -1) {
 
$fontmax = $anode->how_many;
}

$fontsize = ((2*$anode->how_many/$fontmax) + 1 ) * 100;

$output .= " <span style='font-size:".$fontsize."%'>".l($anode->name, '/taxonomy/term/'.$anode->tid)."</span";
}

print
$output;
$output = "";
?>

Popularity by Visits?

quam - May 25, 2005 - 17:06

This is really nice to have. Curious if there has been thought of having weight by popularity of visits to a term/tag? Rather than by popularity of use.

I think I followed the

pamphile - July 26, 2006 - 15:12

I think I followed the installation instructions...

But when I link to http://example.com/term_popular
I see "Popular Terms" I see nothing else.

Any idea why ?

I have two nodes in the database.

Do I have to turnon search ?

http://01ftp.com
http://www.acmetutorials.com

is it possible to do the

hecatomber - November 26, 2005 - 03:23

is it possible to do the same with the tags from technorati.module ?

Not Working...

PiersDesigns - July 22, 2006 - 19:20

I really really want this to work, but I get a Parse Error - /modules/term_popular/term_popular.module on line 7 when I got to my admin/module page to turn it on.

HELP PLEASE!!

I think this is a great module and I would love it on my page.

Why a new module

agentrickard - October 20, 2006 - 14:49

How is this module different from the existing Tagadelic module?

http://drupal.org/project/tagadelic

--
http://ken.blufftontoday.com/
http://new.savannahnow.com/user/2
Search first, ask good questions later.

It's some weeks older and

frjo - October 21, 2006 - 10:05

It's some weeks older and more simpel. I'm sure Tagadelic is the way to go nowadays.

This works with Category.module

marcoBauli - November 18, 2006 - 16:48

..actually Tagadelic does not.

 
 

Drupal is a registered trademark of Dries Buytaert.