public function PollViewForm::buildForm in Poll 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 FormInterface::buildForm
File
- src/
Form/ PollViewForm.php, line 53
Class
- PollViewForm
- Displays banned IP addresses.
Namespace
Drupal\poll\FormCode
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL, $view_mode = 'full') {
// Add the poll to the form.
$form['poll']['#type'] = 'value';
$form['poll']['#value'] = $this->poll;
$form['#view_mode'] = $view_mode;
if ($this
->showResults($this->poll, $form_state)) {
// Check if the user already voted. The form is still being built but
// the Vote button won't be added so the submit callbacks will not be
// called. Directly check for the request method and use the raw user
// input.
if ($request
->isMethod('POST') && $this->poll
->hasUserVoted()) {
$input = $form_state
->getUserInput();
if (isset($input['op']) && $input['op'] == $this
->t('Vote')) {
// If this happened, then the form submission was likely a cached page.
// Force a session for this user so he can see the results.
$this
->messenger()
->addError($this
->t('Your vote for this poll has already been submitted.'));
$_SESSION['poll_vote'][$this->poll
->id()] = FALSE;
}
}
$form['results'] = $this
->showPollResults($this->poll, $view_mode);
// For all view modes except full and block (as block displays it as the
// block title), display the question.
if ($view_mode != 'full' && $view_mode != 'block') {
$form['results']['#show_question'] = TRUE;
}
}
else {
$options = $this->poll
->getOptions();
if ($options) {
$form['choice'] = array(
'#type' => 'radios',
'#title' => t('Choices'),
'#title_display' => 'invisible',
'#options' => $options,
);
}
$form['#theme'] = 'poll_vote';
$form['#entity'] = $this->poll;
$form['#action'] = $this->poll
->toUrl()
->setOption('query', \Drupal::destination()
->getAsArray())
->toString();
// Set a flag to hide results which will be removed if we want to view
// results when the form is rebuilt.
$form_state
->set('show_results', FALSE);
// For all view modes except full and block (as block displays it as the
// block title), display the question.
if ($view_mode != 'full' && $view_mode != 'block') {
$form['#show_question'] = TRUE;
}
}
$form['actions'] = $this
->actions($form, $form_state, $this->poll);
$form['#cache'] = array(
'tags' => $this->poll
->getCacheTags(),
);
return $form;
}