FormTestOptionalContainerForm.php in Drupal 9
File
core/modules/system/tests/modules/form_test/src/Form/FormTestOptionalContainerForm.php
View source
<?php
namespace Drupal\form_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class FormTestOptionalContainerForm extends FormBase {
public function getFormId() {
return 'form_test_optional_container';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['empty_optional'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'empty_optional',
],
],
'#optional' => TRUE,
];
$form['empty_nonoptional'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'empty_nonoptional',
],
],
'#optional' => FALSE,
];
$form['nonempty_optional'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'nonempty_optional',
],
],
'#optional' => TRUE,
];
$form['nonempty_optional']['child_1'] = [];
$form['nonempty_nonoptional'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'nonempty_nonoptional',
],
],
'#optional' => FALSE,
];
$form['nonempty_nonoptional']['child_2'] = [];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}