0

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;
}
1

A token like [field_display_name] in the rewrite results option for the display of a Views field is not the same as a token everywhere else in Drupal, so hook_token_info() and hook_tokens() won't help you. On behalf of Drupal, Views, and the million or so other available words in the English language, i apologize.

Instead you need to extend a Views field handler class and override the document_self_tokens() and add_self_tokens() methods. You can instruct a field to use your custom field handler via hook_views_data_alter() (thanks to Daniel Wehner for this info, and very likely the hook itself).

You can see example code from my need for adding a Views field replacement pattern / token.

If you need help adapting it for your need above let me know and i'll write that here, but the remember to list in your .info the file with your field handler extending class so that Drupal registers it, and it'll work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.