You are here

public function MultiselectTestForm::buildForm in Multiselect 8

Same name and namespace in other branches
  1. 2.x tests/modules/multiselect_test/src/Form/MultiselectTestForm.php \Drupal\multiselect_test\Form\MultiselectTestForm::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

tests/modules/multiselect_test/src/Form/MultiselectTestForm.php, line 23

Class

MultiselectTestForm
Provides test pages for testing the Multiselect module.

Namespace

Drupal\multiselect_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $elements = [];
  $elements['no_default'] = [
    '#title' => 'Multiselect without a default value',
  ];
  $elements['single_default'] = [
    '#title' => 'Multiselect with a default value',
    '#default_value' => 'banana',
  ];
  $elements['multiple_default'] = [
    '#title' => 'Multiselect with multiple default values',
    '#default_value' => [
      'banana',
      'cherry',
    ],
  ];
  $options = [
    'apple' => 'Apple',
    'banana' => 'Banana',
    'cherry' => 'Cherry',
  ];
  foreach ($elements as $key => $element) {

    // Wrap each multiselect in a fieldset.
    $form[$key . '_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $element['#title'] . ' wrapper',
    ];
    $form[$key . '_wrapper'][$key] = $element + [
      '#type' => 'multiselect',
      '#options' => $options,
    ];
  }
  return $form;
}