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.