0

I have created a flag for my comments named HELPFUL. If users find a comment helpful they vote it as such.

The challenge that I have is how do I show the total votes for the flag next to the button? If a comment was voted as helpful 5 times for example, show that next to the helpful button.

If there are no helpful votes for a particular comment show nothing (do not display 0) but only the HELPFUL rate button (flag)

I have tried to add [flag-action:count] and [flag-helpful-count] as the token under the flag setting (/admin/structure/flags/manage/helpful >>edit>> Flag link text) but it does not work.

Looked at COMMENT tokes and I see this available: [comment:flag-helpful-count] It still won't work. It shows this text [comment:flag-helpful-count] instead of a total number.

Any help would be greatly appreciated.

1 Answer 1

1

You try code:

/**
 * Implements hook_tokens().
 */
function mymodule_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if($type == 'comment' && !empty($data['comment'])) {

    foreach($tokens as $name => $original) {
      switch($name) {
        case 'flag-helpful-count':
          //$comment = \Drupal\comment\Entity\Comment::load($data['comment']->get('cid')->value);
          //$count_flag = \Drupal::service('flag.count')->getEntityFlagCounts($comment);
          $replacements[$original] = 10;
          break;
      }
    }
  }
  return $replacements;
}

function mymodule_preprocess_comment(&$variables) {
  $token_service = \Drupal::token();
  $token_count_flag = $token_service->replace('[comment:flag-helpful-count]', array('comment' =>
    $variables['elements']['#entity']));

}
5
  • Thank you for the reply. Which file is this supposed to go in?
    – Progonat
    Commented Aug 24, 2018 at 14:17
  • I think you will need it
    – vinhdv
    Commented Aug 24, 2018 at 14:31
  • I have a custom theme. I am not sure which file to paste the code you provided. Thanks.
    – Progonat
    Commented Aug 24, 2018 at 14:51
  • Any other suggestions?
    – Progonat
    Commented Aug 29, 2018 at 15:14
  • It executes in the custom module
    – vinhdv
    Commented Aug 29, 2018 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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