You are here

function votingapi_tokens_tokens in Voting API 8.3

Implements hook_tokens().

File

modules/votingapi_tokens/votingapi_tokens.tokens.inc, line 58
Contains votingapi_tokens.tokens.inc.

Code

function votingapi_tokens_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];

  // $entity_types = array_keys(\Drupal::entityTypeManager()->getDefinitions());
  $entity_types = _votingapi_tokens_get_entity_types();
  foreach ($entity_types as $entity_type) {
    if ($type == 'votingapi_' . $entity_type . '_token' && !empty($data[$entity_type])) {
      $votes = \Drupal::service('plugin.manager.votingapi.resultfunction')
        ->getResults($entity_type, $data[$entity_type]
        ->id());
      foreach ($tokens as $name => $original) {
        list($token_name, $vote_type) = explode(':', $name);
        switch ($token_name) {
          case 'vote_count':
            $replacements[$original] = $votes[$vote_type]['vote_count'];
            break;
          case 'vote_average':
            $replacements[$original] = $votes[$vote_type]['vote_average'];
            break;
          case 'best_vote':
            $replacements[$original] = max(_votingapi_tokens_get_votes_per_entity($entity_type, $data[$entity_type]
              ->id(), $vote_type));
            break;
          case 'worst_vote':
            $replacements[$original] = min(_votingapi_tokens_get_votes_per_entity($entity_type, $data[$entity_type]
              ->id(), $vote_type));
            break;
        }
      }
    }
  }
  return $replacements;
}