You are here

function template_preprocess_poll_meter in Poll 8

Prepares variables for meter templates.

Default template: poll-meter.html.twig.

Parameters

array $variables: An associative array containing:

  • display_value: The textual representation of the meter bar.
  • form: A string specifying one or more forms to which the <meter> element belongs; multiple forms separated by spaces.
  • attributes: Associative array of attributes to be placed in the meter tag.

File

./poll.module, line 147
Collects votes on different topics in the form of multiple choice questions.

Code

function template_preprocess_poll_meter(&$variables) {
  $poll = $variables['poll'];
  $attributes = $variables['attributes'];
  foreach (array(
    'form',
    'high',
    'low',
    'max',
    'min',
    'optimum',
    'choice',
    'value',
  ) as $key) {
    if (isset($variables[$key])) {

      // This function was initially designed for the <meter> tag, but due to
      // the lack of browser and styling support for it, we're currently using
      // its attributes as HTML5 data attributes.
      $attributes['data-' . $key] = $variables[$key];
    }
  }
  $current_user_vote = \Drupal::service('poll_vote.storage')
    ->getUserVote($poll);
  $options = $poll
    ->getOptions();
  $variables['is_current_selection'] = (int) $current_user_vote['chid'] === array_search($variables['choice'], $options);
  $variables['attributes'] = new Attribute($attributes);
}