You are here

public function FormTestOptionalContainerForm::buildForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/form_test/src/Form/FormTestOptionalContainerForm.php \Drupal\form_test\Form\FormTestOptionalContainerForm::buildForm()

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

core/modules/system/tests/modules/form_test/src/Form/FormTestOptionalContainerForm.php, line 25

Class

FormTestOptionalContainerForm
Builds a simple form to test the #optional property on #type 'container'.

Namespace

Drupal\form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Empty containers.
  $form['empty_optional'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'empty_optional',
      ],
    ],
    '#optional' => TRUE,
  ];
  $form['empty_nonoptional'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'empty_nonoptional',
      ],
    ],
    '#optional' => FALSE,
  ];

  // Non-empty containers
  $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;
}