You are here

public function Select2AjaxForm::buildForm in Select 2 8

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/select2_form_test/src/Form/Select2AjaxForm.php, line 26

Class

Select2AjaxForm
Test form to test the select2 element.

Namespace

Drupal\select2_form_test\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $customize = FALSE) {
  $form['container'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'my-container',
    ],
  ];
  $form['select2_ajax'] = [
    '#type' => 'select2',
    '#title' => 'Ajax',
    '#options' => [],
    '#target_type' => 'entity_test_mulrevpub',
    '#selection_handler' => 'default:entity_test_mulrevpub',
    '#selection_settings' => [
      'target_bundles' => [
        'entity_test_mulrevpub' => 'entity_test_mulrevpub',
      ],
      'auto_create' => TRUE,
      'auto_create_bundle' => 'entity_test_mulrevpub',
    ],
    '#autocreate' => [
      'bundle' => 'entity_test_mulrevpub',
      'uid' => '1',
    ],
    '#multiple' => TRUE,
  ];
  $form['call_ajax'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Call ajax'),
    '#submit' => [
      '::callAjax',
    ],
    '#ajax' => [
      'callback' => '::ajaxCallback',
      'wrapper' => 'my-container',
    ],
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => 'Submit',
  ];
  return $form;
}