You are here

function template_preprocess_fivestar_static in Fivestar 8

Implements hook_preprocess_HOOK() for the Fivestar static output.

File

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

Code

function template_preprocess_fivestar_static(array &$variables) {

  // Get the total number of allowed stars. Defaults to 5.
  $variables['stars'] = !empty($variables['stars']) ? $variables['stars'] : 5;

  // Get number of selected stars.
  $variables['numeric_rating'] = $variables['rating'] / (100 / $variables['stars']);

  // Prepare stars data.
  $stars_data = [];
  for ($i = 1; $i <= $variables['stars']; $i++) {
    $star_value = ceil(100 / $variables['stars'] * $i);
    $prev_star_value = ceil(100 / $variables['stars'] * ($i - 1));
    $stars_data[$i] = [
      'star_value' => $star_value,
      'percent' => NULL,
    ];
    if ($variables['rating'] < $star_value && $variables['rating'] > $prev_star_value) {
      $stars_data[$i]['percent'] = ($variables['rating'] - $prev_star_value) / ($star_value - $prev_star_value) * 100;
    }
  }
  $variables['stars_data'] = $stars_data;
}