You are here

protected function RateWidgetBaseForm::setVotedClass in Rate 8.2

Helper function to set 'rate-voted' html class to radio element.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\votingapi\Entity\Vote $vote_entity: Vote entity.

array $radio_options: Form radio options.

Return value

array The form structure.

2 calls to RateWidgetBaseForm::setVotedClass()
RateWidgetBaseForm::ajaxSubmit in src/Form/RateWidgetBaseForm.php
Ajax submit handler.
RateWidgetBaseForm::buildForm in src/Form/RateWidgetBaseForm.php
Form constructor.

File

src/Form/RateWidgetBaseForm.php, line 583

Class

RateWidgetBaseForm
Form controller for rate vote forms.

Namespace

Drupal\rate\Form

Code

protected function setVotedClass(array $form, Vote $vote_entity, array $radio_options) {
  if (!$vote_entity
    ->isNew()) {
    $vote_value = (int) $vote_entity
      ->getValue();
    foreach ($radio_options as $key => $option) {
      if ($vote_value === $key) {
        $form['value'][$key]['#label_attributes']['class']['rate-voted'] = 'rate-voted';
      }
      else {
        unset($form['value'][$key]['#label_attributes']['class']['rate-voted']);
      }
    }
  }
  else {
    foreach ($radio_options as $key => $option) {
      unset($form['value'][$key]['#label_attributes']['class']['rate-voted']);
    }
  }
  return $form;
}