Hi!
I want to use the voting on our schema.org meta tags, but unfortunately i saw that voting api doesn't provide token for count,average,best and worst rating.
this value are required for meta tags.

Comments

rot3r1 created an issue. See original summary.

useernamee’s picture

I am working on this.

useernamee’s picture

Status: Active » Needs review
StatusFileSize
new7.1 KB

I've made a submodule that adds token support that you need.

nkoporec’s picture

Status: Needs review » Needs work

Tested your patch although it applies and works, there are some minor issues.When applying the patch I get whitespace warnings:

token.patch:54: trailing whitespace.
      $output .= '<p>' . t('This module enables tokens for all entity types 
token.patch:57: trailing whitespace.
      $output .= '<p>' . t('Tokens are dynamic and you have to pass 
token.patch:59: trailing whitespace.
      $output .= t('<ul><li>Vote count :: 
token.patch:61: trailing whitespace.
      $output .= t('<li>Vote average :: 
token.patch:63: trailing whitespace.
      $output .= t('<li>Best vote :: 
warning: squelched 1 whitespace error
warning: 6 lines add whitespace errors.

Also, I think the module should be in modules subdirectory.

useernamee’s picture

Version: 8.x-3.0-alpha6 » 8.x-3.x-dev
Status: Needs work » Needs review
StatusFileSize
new7.13 KB

@nkoporec thanks for your review. I have incorporated your suggestions in this patch.

atobis’s picture

Is this submodule compatible with Drupal 7? I have the same problem but with 7.

proweb.ua’s picture

#5 patch. Tokens do not work

Drupal 8.8.0-rc1
votingapi 8.x-3.x-dev
fivestar 8.x-1.x-dev
schema_metatag 8.x-1.4
metatag 8.x-1.10
PHP 7.3.3

ENTITY_TYPE - node
VOTE_TYPE - vote
my tokens
[votingapi_node_token:vote_count:vote]
[votingapi_node_token:vote_average:vote]
[votingapi_node_token:best_vote:vote]
[votingapi_node_token:worst_vote:vote]

proweb.ua’s picture

$entity_types = get_entity_types();

get_entity_types() do not work

$entity_types = array_keys(\Drupal::entityTypeManager()->getDefinitions());

works

shubham.prakash’s picture

StatusFileSize
new7.21 KB

Updated the patch for the above fix.

proweb.ua’s picture

If the rating has not yet changed

Notice: Undefined index: vote in votingapi_tokens_tokens() (line 74 of modules/custom/votingapi_tokens/votingapi_tokens.tokens.inc).
Notice: Undefined index: vote in votingapi_tokens_tokens() (line 79 of modules/custom/votingapi_tokens/votingapi_tokens.tokens.inc).

$replacements[$original] = $votes[$vote_type]['vote_count'];
$replacements[$original] = round($votes[$vote_type]['vote_average'] / 20, 1);

need to add variable check

if (!empty($votes[$vote_type]['vote_average'])) {
  $replacements[$original] = $votes[$vote_type]['vote_count'];
}
if (!empty($votes[$vote_type]['vote_average'])) {
  $replacements[$original] = round($votes[$vote_type]['vote_average'] / 20, 1);
}

and also

if (!empty($votes[$vote_type]['vote_average'])) {
  $replacements[$original] = max(voting_api_votes($entity_type, $data[$entity_type]->id(), $vote_type));
}
if (!empty($votes[$vote_type]['vote_average'])) {
  $replacements[$original] = min(voting_api_votes($entity_type, $data[$entity_type]->id(), $vote_type));
}
useernamee’s picture

get_entity_types() function was supposed to get possible entity types that have vote fields to be voted on.

getDefinitions() gets you all possible entity types so it is not suitable here.

I've now written a SQL query that goes through votes and gets unique entity types from there.

useernamee’s picture

alex_optim’s picture

Status: Needs review » Reviewed & tested by the community

Good work.

pifagor’s picture

  • pifagor committed b55f747 on 8.x-3.x authored by useernamee
    Issue #2961079 by useernamee, shubham.prakash, alex_optim, pifagor:...
pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.