Displaying clickable popular tags

Displaying clickable popular tags

Submitted by Jitesh Doshi on Fri, 06/01/2012 - 10:32

<p>&nbsp; &nbsp; We recently worked on creating a list of tags most frequently used by visitors (it is a question-answer forum), with the number of tags specified by the site administrator, with a choice of '0' or a blank field indicating that all terms from a vocabulary be displayed. An important part of the implementation was generating the correct database query. After a couple of attempts, the following module was written (it is a hook_form_alter with the alteration being applied to the taxonomy through this function):</p><p><span style="background-color: #c0c0c0;"><code>{syntaxhighlighter brush:php;}</code></span></p><p><span style="background-color: #c0c0c0;"><code>define('SQL_POPULAR_TERMS', 'SELECT td.tid, td.name, COUNT(tn.tid) FROM term_data td LEFT JOIN term_node tn ON (tn.tid = td.tid) WHERE td.vid = %d GROUP BY td.tid ORDER BY COUNT(tn.tid) DESC LIMIT %d');</code></span></p><p><span style="background-color: #c0c0c0;"><code>define('SQL_POPULAR_TERMS_UNLIMITED', 'SELECT td.tid, td.name, COUNT(tn.tid) FROM term_data td LEFT JOIN term_node tn ON (tn.tid = td.tid) WHERE td.vid = %d GROUP BY td.tid ORDER BY COUNT(tn.tid) DESC');</code></span></p><p><span style="background-color: #c0c0c0;"><code>function _sqe_get_popular_terms($vid, $count = NULL) { if(is_null($count)) { $count = variable_get('sqe_popular_tag_limit', 10); }</code></span></p><p><span style="background-color: #c0c0c0;"><code>if(empty($count)) { $query = db_query(SQL_POPULAR_TERMS_UNLIMITED, $vid); }</code></span></p><p><span style="background-color: #c0c0c0;"><code>else { $query = db_query(SQL_POPULAR_TERMS, $vid, $count); }</code></span></p><p><span style="background-color: #c0c0c0;"><code>$terms = array();</code></span></p><p><span style="background-color: #c0c0c0;"><code>while ($row = db_fetch_array($query)) { $terms[$row['tid']] = $row['name']; }</code></span></p><p><span style="background-color: #c0c0c0;"><code>return $terms; }</code></span></p><p><span style="background-color: #c0c0c0;"><code>{/syntaxhighlighter}</code></span></p><p>&nbsp; &nbsp; With a little bit of styling (we made the tags look like buttons and added an element type 'a' to them to make the default behaviour on hover appear to be 'click'), the tags look invitingly clickable.</p>

Jitesh Doshi

Profile picture for user Jitesh Doshi
Managing Partner & CTO
  • A seasoned technology entrepreneur and enthusiast
  • A regular speaker at industry conferences and universities
  • Host and organizer of technology user groups
  • Active in management of non-profit organizations serving the local community
  • Leader and contributor for multiple open-source projects
  • Expert in cloud, application integration, web and mobile technologies
  • Author of open-source projects, including on Drupal.org - Popular Tags and PRLP.
  • Developed several highly successful software platforms and frameworks for clients