Drupal alter tagcloud markup

The Tagclouds module is a simple way to put a tag cloud on your site. Here's how to alter (wrap and reorganize) the markup from this:

<span class="tagclouds-term"><a href="/tags/actionscript" class="tagclouds level1" title="">Actionscript</a></span>
<span class="tagclouds-term"><a href="/tags/bash" class="tagclouds level5" title="">Bash</a></span>

to this:

<div id="tagclouds">
  <a href="/tags/actionscript" class="tagclouds level1" title="">Actionscript</a>
  <a href="/tags/bash" class="tagclouds level5" title="">Bash</a>
</div>

In your theme's template.php:

<?php
/**
 * Implements theme_tagclouds_weighted().
 */
function YOURTHEME_tagclouds_weighted(array $vars) {
  $terms = $vars['terms'];
 
  $output = '<div id="tagclouds">';
  $display = variable_get('tagclouds_display_type', 'style');
 
  if (module_exists('i18n_taxonomy')) {
    $language = i18n_language();
  }
 
  if ($display=='style') {
    foreach ($terms as $term) {
      if (module_exists('i18n_taxonomy')) {
        $term_name = i18n_taxonomy_term_name($term, $language->language);
        $term_desc = tagclouds_i18n_taxonomy_term_description($term, $language->language);
      } else {
        $term_name = $term->name;
        $term_desc = $term->description;
      }
      $output .= YOURTHEME_tagclouds_display_term_link_weight_override($term_name, $term->tid, $term->weight, $term_desc);
    }
  }
  else {
    foreach ($terms as $term) {
      if (module_exists('i18n_taxonomy')) {
        $term_name = i18n_taxonomy_term_name($term, $language->language);
        $term_desc = tagclouds_i18n_taxonomy_term_description($term, $language->language);
      } else {
        $term_name = $term->name;
        $term_desc = $term->description;
      }
      if ($term->count==1 && variable_get("tagclouds_display_node_link", false)) {
         $output .= tagclouds_display_node_link_count($term_name, $term->tid, $term->nid, $term->count, $term_desc);
      }
      else {
         $output .= tagclouds_display_term_link_count($term_name, $term->tid, $term->count, $term_desc);
      }
    }
  }
  $output .= '</div>';
  return $output;
}
 
/**
 * Display Single Tag with Style
 * 
 * @see tagclouds_display_term_link_weight().
 */
function YOURTHEME_tagclouds_display_term_link_weight_override($name, $tid, $weight, $description) {
  if ($term = taxonomy_term_load($tid)) {
    $uri = entity_uri('taxonomy_term', $term);
    $uri['options']['attributes']['class'][] = 'tagclouds';
    $uri['options']['attributes']['class'][] = 'level' . $weight;
    $uri['options']['attributes']['title'] = $description;
    return l($name, $uri['path'], $uri['options']) . "\n";
  }
}
?>

Tags

Article Type

General