You are here

protected function PollViewForm::actions in Poll 8

1 call to PollViewForm::actions()
PollViewForm::buildForm in src/Form/PollViewForm.php
Form constructor.

File

src/Form/PollViewForm.php, line 167

Class

PollViewForm
Displays banned IP addresses.

Namespace

Drupal\poll\Form

Code

protected function actions(array $form, FormStateInterface $form_state, $poll) {
  $actions = [];

  // Default ajax behavior, use the poll URL for faster submission, this
  // requires that we explicitly provide the ajax_form query argument too in
  // the separate options key, as that replaces all options of the Url object.
  $ajax = [
    'callback' => '::ajaxReplaceForm',
    'url' => $this->poll
      ->toUrl(),
    'options' => [
      'query' => [
        FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
        'view_mode' => $form['#view_mode'],
      ],
    ],
  ];
  if ($this
    ->showResults($poll, $form_state)) {

    // Allow user to cancel their vote.
    if ($this
      ->isCancelAllowed($poll)) {
      $actions['#type'] = 'actions';
      $actions['cancel']['#type'] = 'submit';
      $actions['cancel']['#button_type'] = 'primary';
      $actions['cancel']['#value'] = t('Cancel vote');
      $actions['cancel']['#submit'] = array(
        '::cancel',
      );
      $actions['cancel']['#ajax'] = $ajax;
      $actions['cancel']['#weight'] = '0';
    }
    if (!$poll
      ->hasUserVoted() && $poll
      ->isOpen() && $poll
      ->getAnonymousVoteAllow()) {
      $actions['#type'] = 'actions';
      $actions['back']['#type'] = 'submit';
      $actions['back']['#button_type'] = 'primary';
      $actions['back']['#value'] = t('View poll');
      $actions['back']['#submit'] = array(
        '::back',
      );
      $actions['back']['#ajax'] = $ajax;
      $actions['back']['#weight'] = '0';
    }
  }
  else {
    $actions['#type'] = 'actions';
    $actions['vote']['#type'] = 'submit';
    $actions['vote']['#button_type'] = 'primary';
    $actions['vote']['#value'] = t('Vote');
    $actions['vote']['#validate'] = array(
      '::validateVote',
    );
    $actions['vote']['#submit'] = array(
      '::save',
    );
    $actions['vote']['#ajax'] = $ajax;
    $actions['vote']['#weight'] = '0';

    // View results before voting.
    if ($poll->result_vote_allow->value || $this
      ->currentUser()
      ->hasPermission('view poll results')) {
      $actions['result']['#type'] = 'submit';
      $actions['result']['#button_type'] = 'primary';
      $actions['result']['#value'] = t('View results');
      $actions['result']['#submit'] = array(
        '::result',
      );
      $actions['result']['#ajax'] = $ajax;
      $actions['result']['#weight'] = '1';
    }
  }
  return $actions;
}