You are here

function template_preprocess_fivestar_formatter_rating in Fivestar 8

Implements hook_preprocess_HOOK() for the Fivestar rating formatter.

File

./fivestar.module, line 243
A simple n-star voting widget, usable in other forms.

Code

function template_preprocess_fivestar_formatter_rating(array &$variables) {
  $element = $variables['element'];

  // Get number of stars being used. Usually 5 ...
  $stars = $element['#instance_settings']['stars'];
  if (empty($stars)) {
    $stars = 5;
  }

  // Set stars variable.
  $variables['stars'] = $stars;

  // Get average, which ranges from 0 to 100.
  $average = $element['#item']['average'];
  if (empty($average)) {
    $average = 0;
  }

  // Set average variable.
  $variables['average'] = round($average, 1);

  // Rating is # out of available stars.
  // For example, if the average vote is 2 out of 5 stars, the rating
  // would be 2.
  $rating = round($average / 100 * $stars, 1);
  $variables['rating'] = $rating;
}