Displaying flag count in Drupal node bookmark link

Wondering how to show the number of times a node has been bookmarked? Follow these simple steps and you are set
1. Create a module folder in your modules directory, typically sites/all/modules called flag_alter
2. Create text file called flag_alter.info
3. Enter the following in the flag_alter.info and save it

name = Flag Alter
package = Flags
description = Adds count capability to Flags
dependencies[] = flag
core = 6.x

4. Now create another text file called flag_alter.module
5. Enter the following code in it and save it

/**
* Implements hook_link_alter()
*/
function flag_alter_link_alter(&$links, $node, $comment = NULL) {
if (isset($links['flag-bookmarks'])) {
//find out the number of bookmarks for this node
$result = db_result(db_query("SELECT count FROM {flag_counts} WHERE content_type ='node' AND content_id = %d", $node->nid));
if (empty($result)) {
$result = 0;
}
$links['flag-bookmarks']['title'] .= "($result)";
}
}


6. Enable the module flag_alter. Now you'll see the number of times a node has been bookmarked by users.

Post new comment