3 namespace Drupal\js_ajax_test\Form;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\js_ajax_test\Ajax\JsAjaxTestCommand;
11 * Test form for js_ajax_test
15 class JsAjaxTestForm extends FormBase {
20 public function getFormId() {
21 return 'js_ajax_test_form';
25 * Form for testing the addition of various types of elements via Ajax.
27 public function buildForm(array $form, FormStateInterface $form_state) {
28 $form['#attached']['library'][] = 'js_ajax_test/ajax';
29 $form['custom']['#prefix'] = '<div id="js_ajax_test_form_wrapper">';
30 $form['custom']['#suffix'] = '</div>';
32 // Button to test for the waitForButton() assertion.
33 $form['test_button'] = [
35 '#value' => $this->t('Add button'),
36 '#button_type' => 'primary',
38 'callback' => [static::class, 'addButton'],
43 'wrapper' => 'js_ajax_test_form_wrapper',
50 * Ajax callback for the "Add button" button.
52 public static function addButton(array $form, FormStateInterface $form_state) {
53 return (new AjaxResponse())
54 ->addCommand(new JsAjaxTestCommand());
60 public function submitForm(array &$form, FormStateInterface $form_state) {
61 // An empty implementation, as we never submit the actual form.