View source
<?php
namespace Drupal\ajax_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
class DynamicFormSections extends FormBase {
public function getFormId() {
return 'ajax_example_dynamic_sections';
}
public function buildForm(array $form, FormStateInterface $form_state, $nojs = NULL) {
$form['#attached']['library'][] = 'ajax_example/ajax_example.library';
$form['info'] = [
'#markup' => $this->t('<p>Like other examples in this module, this form has a path that
can be modified with /nojs to simulate its behavior without JavaScript.
</p><ul>
<li>@try_it_without_ajax</li>
<li>@try_it_with_ajax</li>
</ul>', [
'@try_it_without_ajax' => Link::createFromRoute($this->t('Try it without AJAX'), 'ajax_example.dynamic_form_sections', [
'nojs' => 'nojs',
])
->toString(),
'@try_it_with_ajax' => Link::createFromRoute($this->t('Try it with AJAX'), 'ajax_example.dynamic_form_sections')
->toString(),
]),
];
$form['question_type_select'] = [
'#type' => 'select',
'#title' => $this->t('Question style'),
'#options' => [
'Choose question style' => 'Choose question style',
'Multiple Choice' => 'Multiple Choice',
'True/False' => 'True/False',
'Fill-in-the-blanks' => 'Fill-in-the-blanks',
],
'#ajax' => [
'wrapper' => 'questions-fieldset-wrapper',
'callback' => '::promptCallback',
],
];
$form['question_type_submit'] = [
'#type' => 'submit',
'#value' => $this->t('Choose'),
'#attributes' => [
'class' => [
'ajax-example-inline',
],
],
'#limit_validation_errors' => [],
'#validate' => [],
];
if ($nojs != 'nojs') {
$form['question_type_submit']['#attributes']['class'][] = 'js-hide';
}
else {
unset($form['question_type_select']['#ajax']);
}
$form['questions_fieldset'] = [
'#type' => 'details',
'#title' => $this->t('Stuff will appear here'),
'#open' => TRUE,
'#attributes' => [
'id' => 'questions-fieldset-wrapper',
'class' => [
'questions-wrapper',
],
],
];
$question_type = $form_state->getValue('question_type_select');
if (!empty($question_type) && $question_type !== 'Choose question style') {
$form['questions_fieldset']['question'] = [
'#markup' => $this->t('Who was the first president of the U.S.?'),
];
switch ($question_type) {
case 'Multiple Choice':
$form['questions_fieldset']['question'] = [
'#type' => 'radios',
'#title' => $this->t('Who was the first president of the United States'),
'#options' => [
'George Bush' => 'George Bush',
'Adam McGuire' => 'Adam McGuire',
'Abraham Lincoln' => 'Abraham Lincoln',
'George Washington' => 'George Washington',
],
];
break;
case 'True/False':
$form['questions_fieldset']['question'] = [
'#type' => 'radios',
'#title' => $this->t('Was George Washington the first president of the United States?'),
'#options' => [
'George Washington' => 'True',
0 => 'False',
],
'#description' => $this->t('Click "True" if you think George Washington was the first president of the United States.'),
];
break;
case 'Fill-in-the-blanks':
$form['questions_fieldset']['question'] = [
'#type' => 'textfield',
'#title' => $this->t('Who was the first president of the United States'),
'#description' => $this->t('Please type the correct answer to the question.'),
];
break;
}
$form['questions_fieldset']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit your answer'),
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$messenger = $this->messenger();
if ($form_state->getValue('question_type_submit') == 'Choose') {
$form_state->setValue('question_type_select', $form_state->getUserInput()['question_type_select']);
$form_state->setRebuild();
}
if ($form_state->getValue('submit') == 'Submit your answer') {
$form_state->setRebuild(FALSE);
$answer = $form_state->getValue('question');
if ($answer == 1 && $form['questions_fieldset']['question']['#type'] == 'checkbox') {
$answer = $form['questions_fieldset']['question']['#title'];
}
if ($answer == $this->t('George Washington')) {
$messenger->addMessage($this->t('You got the right answer: @answer', [
'@answer' => $answer,
]));
}
else {
$messenger->addMessage($this->t('Sorry, your answer (@answer) is wrong', [
'@answer' => $answer,
]));
}
return;
}
$form_state->setRebuild();
}
public function promptCallback(array $form, FormStateInterface $form_state) {
return $form['questions_fieldset'];
}
}