JsAjaxTestForm.php in Drupal 9
File
core/modules/system/tests/modules/js_ajax_test/src/Form/JsAjaxTestForm.php
View source
<?php
namespace Drupal\js_ajax_test\Form;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\js_ajax_test\Ajax\JsAjaxTestCommand;
class JsAjaxTestForm extends FormBase {
public function getFormId() {
return 'js_ajax_test_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'js_ajax_test/ajax';
$form['custom']['#prefix'] = '<div id="js_ajax_test_form_wrapper">';
$form['custom']['#suffix'] = '</div>';
$form['test_button'] = [
'#type' => 'submit',
'#value' => $this
->t('Add button'),
'#button_type' => 'primary',
'#ajax' => [
'callback' => [
static::class,
'addButton',
],
'progress' => [
'type' => 'throbber',
'message' => NULL,
],
'wrapper' => 'js_ajax_test_form_wrapper',
],
];
return $form;
}
public static function addButton(array $form, FormStateInterface $form_state) {
return (new AjaxResponse())
->addCommand(new JsAjaxTestCommand());
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}