Select2OptgroupForm.php in Select 2 8
File
tests/modules/select2_form_test/src/Form/Select2OptgroupForm.php
View source
<?php
namespace Drupal\select2_form_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class Select2OptgroupForm extends FormBase {
public function getFormId() {
return 'form_test_select2_optgroup';
}
public function buildForm(array $form, FormStateInterface $form_state, $customize = FALSE) {
$form['select2_optgroups'] = [
'#type' => 'select2',
'#title' => 'Optgroups',
'#options' => [
0 => 'Foo',
1 => 'Bar',
'Baba' => [
3 => 'Nana',
],
2 => 'Gaga',
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->setResponse(new JsonResponse($form_state
->getValues()));
}
}