You are here

public function BaseRatingForm::buildForm in Votingapi Widgets 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/BaseRatingForm.php, line 71

Class

BaseRatingForm
Form controller for Campaign edit forms.

Namespace

Drupal\votingapi_widgets\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $entity = $this
    ->getEntity();
  $result_function = $this
    ->getResultFunction($form_state);
  $options = $form_state
    ->get('options');
  $form_id = Html::getUniqueId('vote-form');
  $plugin = $form_state
    ->get('plugin');
  $settings = $form_state
    ->get('settings');
  $form['#cache']['contexts'][] = 'user.permissions';
  $form['#cache']['contexts'][] = 'user.roles:authenticated';
  $form['#attributes']['id'] = $form_id;
  $form['value'] = [
    '#type' => 'select',
    '#options' => $options,
    '#attributes' => [
      'autocomplete' => 'off',
      'data-result-value' => $this
        ->getResults($result_function) ? $this
        ->getResults($result_function) : -1,
      'data-vote-value' => $entity
        ->getValue() ? $entity
        ->getValue() : ($this
        ->getResults($result_function) ? $this
        ->getResults($result_function) : -1),
      'data-style' => $settings['style'] ? $settings['style'] : 'default',
    ],
  ];
  $form['value']['#attributes']['data-show-own-vote'] = 'true';
  $form['value']['#default_value'] = (int) $entity
    ->getValue();
  if (!$settings['show_own_vote']) {
    $form['value']['#attributes']['data-show-own-vote'] = 'false';
    $form['value']['#default_value'] = $this
      ->getResults($result_function);
  }
  if ($settings['readonly'] || !$plugin
    ->canVote($entity)) {
    $form['value']['#attributes']['disabled'] = 'disabled';
  }
  if ($settings['show_results']) {
    $form['result'] = [
      '#theme' => 'container',
      '#attributes' => [
        'class' => [
          'vote-result',
        ],
      ],
      '#children' => [],
      '#weight' => 100,
    ];
    $form['result']['#children']['result'] = $plugin
      ->getVoteSummary($entity);
  }
  $form['submit'] = $form['actions']['submit'];
  $form['actions']['#access'] = FALSE;
  $form['submit'] += [
    '#type' => 'button',
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxSubmit',
      ],
      'event' => 'click',
      'wrapper' => $form_id,
      'progress' => [
        'type' => NULL,
      ],
    ],
  ];
  return $form;
}