diff --git a/fivestar.tokens.inc b/fivestar.tokens.inc new file mode 100644 index 0000000..7c2a137 --- /dev/null +++ b/fivestar.tokens.inc @@ -0,0 +1,72 @@ +getFieldMapByFieldType('fivestar'); + + foreach (array_keys($fivestar_field_map) as $entity_type_id) { + if (!array_key_exists($entity_type_id, $info['tokens'])) { + continue; + } + + $info['tokens'][$entity_type_id]['fivestar-rating'] = [ + 'name' => t('Fivestar rating'), + ]; + $info['tokens'][$entity_type_id]['fivestar-count'] = [ + 'name' => t('Fivestar vote sum'), + ]; + } +} + +/** + * Implements hook_tokens(). + */ +function fivestar_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { + if ( + !array_key_exists('fivestar-rating', $tokens) && + !array_key_exists('fivestar-count', $tokens) + ) { + return []; + } + + if ( + !array_key_exists($type, $data) || + !$data[$type] instanceof FieldableEntityInterface + ) { + return []; + } + + $replacements = []; + $results = \Drupal::service('fivestar.vote_result_manager')->getResults($data[$type]); + foreach ([ + 'fivestar-rating' => 'vote_average', + 'fivestar-count' => 'vote_count', + ] as $token => $result_property) { + if (!array_key_exists($token, $tokens)) { + continue; + } + if ( + !array_key_exists('vote', $results) || + !array_key_exists($result_property, $results['vote']) + ) { + $value = '0'; + } + else { + $value = $results['vote'][$result_property]; + } + $replacements[$tokens[$token]] = $value; + } + + return $replacements; +}