You are here

function template_preprocess_rate_template_fivestar in Rate 8

Implements hook_preprocess_HOOK() for rate-template-fivestar.html.twig.

File

./rate.module, line 216
Hook implementation code for the Rate module.

Code

function template_preprocess_rate_template_fivestar(&$variables) {
  $variables['stars'] = [];

  // Calculate the score.
  $score = isset($variables['results']['fivestar']['vote_average']) ? $variables['results']['fivestar']['vote_average'] : 0;

  // Set up the links.
  for ($counter = 1; $counter < 6; $counter++) {
    $variables['stars'][$counter]['star_attributes'] = new Attribute();
    $variables['stars'][$counter]['star_attributes']['class'] = [];
    if ($counter <= $score) {
      $variables['stars'][$counter]['star_attributes']['class'][] = 'rate-fivestar-btn-filled';
    }
    else {
      $variables['stars'][$counter]['star_attributes']['class'][] = 'rate-fivestar-btn-empty';
    }
    $variables['stars'][$counter]['star_attributes']['class'][] = ' rate-fivestar-' . $counter;
    if ($variables['use_ajax'] && !$variables['has_voted'] && $variables['can_vote']) {
      $variables['stars'][$counter]['star_attributes']['class'][] = 'use-ajax';
    }
    if ($variables['entity_id']) {
      $variables['stars'][$counter]['star_link'] = Url::fromRoute('rate.vote', [
        'entity_type_id' => $variables['entity_type_id'],
        'vote_type_id' => 'fivestar',
        'value' => $counter,
        'entity_id' => $variables['entity_id'],
        'widget_type' => $variables['widget_type'],
        'destination' => \Drupal::destination()
          ->getAsArray()['destination'],
      ]);
    }
  }
  _rate_set_common_theme_variables($variables, FALSE);
  _rate_get_undo_link($variables);
  $variables['#attached']['library'][] = 'rate/fivestar';
}