You are here

public function VotingApiReactionForm::buildForm in Voting API Reaction 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/VotingApiReactionForm.php, line 94

Class

VotingApiReactionForm
Form implementation of the reaction form used in field formatter.

Namespace

Drupal\votingapi_reaction\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Prepare settings.
  $field_items = $form_state
    ->get('field_items');
  $settings = $form_state
    ->get('formatter_settings') + $field_items
    ->getSettings();

  // Check form status and user access to the form.
  $form = parent::buildForm($form, $form_state);
  $form['#id'] = Html::getUniqueId('votingapi_reaction_form');
  $form['#attached']['library'][] = 'votingapi_reaction/scripts';
  $form['#attributes']['class'][] = 'votingapi-reaction-form';
  $form['#attributes']['autocomplete'] = 'off';
  $form['#cache']['max-age'] = 0;

  // Try to get the last reaction.
  if ($entity = $this->reactionManager
    ->lastReaction($this->entity, $settings)) {
    $this->entity = $entity;
  }

  // Check user access and form status.
  $status = $this
    ->checkStatus($form_state);
  $access = $this
    ->checkAccess($form_state, $this->entity);
  $form['#access'] = !is_null($status) && !is_null($access);

  // Display summary.
  $results = $this->reactionManager
    ->getResults($this->entity, $settings);
  if ($settings['show_summary']) {
    $total = array_sum(array_column($results, 'vote_sum'));
    $form['summary'] = [
      '#type' => '#markup',
      '#markup' => $this
        ->formatPlural($total, '@count reaction', '@count reactions'),
    ];
  }

  // Display reactions.
  $form['type'] = [
    '#type' => 'radios',
    '#options' => $this->reactionManager
      ->getReactions($settings, $results),
    '#default_value' => $this->entity
      ->bundle(),
    '#id' => $form['#id'] . '-vote',
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxSubmitForm',
      ],
      'event' => 'click',
      'wrapper' => $form['#id'],
      'progress' => [
        'type' => NULL,
        'message' => NULL,
      ],
    ],
    '#disabled' => !$status || !$access,
  ];

  // Store reactions order, so we can persist it for AJAX.
  $form['reactions'] = [
    '#type' => 'value',
    '#value' => $form_state
      ->hasValue('reactions') ? $form_state
      ->getValue('reactions') : array_keys($form['type']['#options']),
  ];

  // Re-purpose entity submit button.
  $form['actions'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#noscript' => TRUE,
    '#weight' => 1,
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
    '#disabled' => !$status || !$access,
    '#submit' => [
      '::submitForm',
    ],
  ];
  $form['actions']['reset'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Reset'),
    '#disabled' => !$status || !$access,
  ];
  return $form;
}