public function PollViewForm::save in Poll 8
Save a user's vote submit function.
Parameters
array $form:
\Drupal\Core\Form\FormStateInterface $form_state:
File
- src/
Form/ PollViewForm.php, line 339
Class
- PollViewForm
- Displays banned IP addresses.
Namespace
Drupal\poll\FormCode
public function save(array $form, FormStateInterface $form_state) {
$options = array();
$options['chid'] = $form_state
->getValue('choice');
$options['uid'] = $this
->currentUser()
->id();
$options['pid'] = $form_state
->getValue('poll')
->id();
$options['hostname'] = \Drupal::request()
->getClientIp();
$options['timestamp'] = \Drupal::time()
->getRequestTime();
// Save vote.
/** @var \Drupal\poll\PollVoteStorage $vote_storage */
$vote_storage = \Drupal::service('poll_vote.storage');
$vote_storage
->saveVote($options);
$this
->messenger()
->addMessage($this
->t('Your vote has been recorded.'));
if ($this
->currentUser()
->isAnonymous()) {
// The vote is recorded so the user gets the result view instead of the
// voting form when viewing the poll. Saving a value in $_SESSION has the
// convenient side effect of preventing the user from hitting the page
// cache. When anonymous voting is allowed, the page cache should only
// contain the voting form, not the results.
$_SESSION['poll_vote'][$form_state
->getValue('poll')
->id()] = $form_state
->getValue('choice');
}
// In case of an ajax submission, trigger a form rebuild so that we can
// return an updated form through the ajax callback.
if ($this
->getRequest()->query
->get('ajax_form')) {
$form_state
->setRebuild(TRUE);
}
// No explicit redirect, so that we stay on the current page, which might
// be the poll form or another page that is displaying this poll, for
// example as a block.
}