Below is the code I am trying to get implemented for a custom token module. The purpose of the token is to work in conjunction with the [field_display_name] token for the comment author's name for a recent comment block (created be Views). I am using tokens in the rewrite results option of (Author) User:link Field in the View. The plan was to add this new token to the comment tokens so that it would show up in the View field's rewrite results replacement patterns. The token replacement pattern would be [field_display_name][commenter-count], with the intended final output being one of the following options:
No additional commenters -> just the comment author's name i.e. "John Smith" One additional commenter -> "John Smith & 1 other" More than one additional commenter -> "John Smith & 2 others"
I have enabled the module, and I am not getting the new token showing up in the replacement pattern options. I'm not sure if this is a code issue or a Views settings issue. I am suspicious that the argument ($nid) for the commenter_count API function to perform the query is not being passed in from the current node. Any help would be appreciated.
Thank you in advance.
<?php
/**
* @file
* Extends the comment modules tokens to include a token (commenter-count).
* To be used in conjunction with the [comment:author] token.
*
* When Enabled this module creates a token that counts all commenters
* of a node and returns one of 4 possible strings:
*
* Case 1 - No commenter - returns False.
* Case 2 - 1 commenter - returns Null.
* Case 3 - 2 commenters - returns string: "and 1 other".
* Case 4 - 2 commenters - returns string: "and # others".
*/
/**
* Get number of commenters for a specific node.
*
* @param $nid
* Node-id to count commenters for.
* @return $commenter_count result an integer, or FALSE on error.
*/
function commenter_count($nid) {
if (isset($nid)) {
// Retrieve the number of commenters for the current node-id.
$results = db_query('SELECT COUNT(DISTINCT uid) FROM {comment} WHERE nid = :nid', array(':nid'=> $nid))->fetchField();
if ($results->uid < 1) {
$commenter_count = '';
}
elseif ($results->uid == 1) {
$commenter_count = t('& 1 other');
}
else {
$commenter_count = t('& @num others', array('@num' => $results->uid));
}
return $commenter_count;
}
else {
return FALSE;
}
}
/**
* Implements hook_token_info().
*/
function commentor_count_token_info() {
$info['tokens']['comment']['commenter-count'] = array(
'name' => t('Commenter count'),
'description' => t('This token counts the number of commenters of a given node.')
);
}
/**
* Implements hook_tokens().
*
*/
function commenter_count_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array('absolute' => TRUE);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'custom') {
foreach ($tokens as $name => $original) {
switch ($name){
case 'commenter-count':
$replacements[$original] = commenter_count($node->nid);
break;
}
}
}
return $replacements;
}