You are here

function template_preprocess_fivestar_summary in Fivestar 8

Implements hook_preprocess_HOOK() for the Fivestar summary output.

File

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

Code

function template_preprocess_fivestar_summary(array &$variables) {
  $votes = $variables['votes'];
  $stars_number = $variables['stars'];
  $user_rating = $variables['user_rating'];
  $average_rating = $variables['average_rating'];
  $show_votes = $votes !== NULL;
  $show_user_rating = $user_rating !== NULL;
  $show_average_rating = $average_rating !== NULL;
  $variables['average_rating_microdata'] = '';
  $variables['rating_count_microdata'] = '';
  if ($show_user_rating) {
    $output_type = $show_votes ? 'user-count' : 'user';
    if ($user_rating > 0) {
      $variables['user_stars'] = round($user_rating * $stars_number / 100, 1);
    }
    else {
      $variables['user_stars'] = t('None');
    }
  }
  if ($show_average_rating) {
    if ($show_user_rating) {
      $output_type = 'combo';
    }
    else {
      $output_type = $show_votes ? 'average-count' : 'average';
    }
    $variables['average_stars'] = round($average_rating * $stars_number / 100, 1);
    if (!empty($variables['microdata']['average_rating']['#attributes'])) {
      $variables['average_rating_microdata'] = new Attribute($variables['microdata']['average_rating']['#attributes']);
    }
  }
  if ($show_votes) {
    if (!$show_user_rating && !$show_average_rating) {
      $output_type = 'count';
    }
    if ($votes > 0) {
      if (!empty($variables['microdata']['rating_count']['#attributes'])) {
        $variables['rating_count_microdata'] = new Attribute($variables['microdata']['rating_count']['#attributes']);
      }
    }
  }
  $variables['output_type'] = $output_type ?? '';
}