You are here

function template_preprocess_rate_template_thumbs_up_down in Rate 8

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

File

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

Code

function template_preprocess_rate_template_thumbs_up_down(&$variables) {
  $vote_types = [
    'up' => '1',
    'down' => '-1',
  ];
  foreach ($vote_types as $vote_type => $value) {
    $variables[$vote_type . '_li_attributes'] = new Attribute();
    $variables[$vote_type . '_li_attributes']['class'] = [];
    $variables[$vote_type . '_li_attributes']['class'] = 'thumb-' . $vote_type;
    $variables[$vote_type . '_attributes'] = new Attribute();
    $variables[$vote_type . '_attributes']['class'] = [];
    $variables[$vote_type . '_attributes']['class'][] = 'rate-thumbs-up-down-btn-' . $vote_type;

    // Add a class if the current user has voted on this item.
    if ($variables['user_voted'] === $value) {
      $variables[$vote_type . '_li_attributes']['class'][] = 'rate-voted';
    }
    if ($variables['use_ajax'] && !$variables['has_voted'] && $variables['can_vote']) {
      $variables[$vote_type . '_attributes']['class'][] = 'use-ajax';
    }
  }
  $variables['percent_attributes'] = new Attribute();
  $variables['percent_attributes']['class'] = [];
  $variables['percent_attributes']['class'][] = 'percent';
  $total_votes = isset($variables['results']['updown']['vote_count']) ? $variables['results']['updown']['vote_count'] : 0;
  $variables['up_votes'] = isset($variables['results']['updown']['rate_count_up']) ? $variables['results']['updown']['rate_count_up'] : 0;
  $variables['down_votes'] = $total_votes - $variables['up_votes'];
  if (!empty($total_votes)) {
    $variables['up_percent'] = round($variables['up_votes'] / $total_votes * 100);
    $variables['down_percent'] = round($variables['down_votes'] / $total_votes * 100);
  }
  else {
    $variables['up_percent'] = 0;
    $variables['down_percent'] = 0;
  }
  _rate_set_common_theme_variables($variables);
  _rate_get_undo_link($variables);
  $variables['#attached']['library'][] = 'rate/thumbs-up-down';
}