3 namespace Drupal\ajax_forms_test;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\DataCommand;
7 use Drupal\Core\Ajax\HtmlCommand;
8 use Drupal\Core\Form\FormStateInterface;
11 * Simple object for testing methods as Ajax callbacks.
16 * Ajax callback triggered by select.
18 public function selectCallback($form, FormStateInterface $form_state) {
19 $response = new AjaxResponse();
20 $response->addCommand(new HtmlCommand('#ajax_selected_color', $form_state->getValue('select')));
21 $response->addCommand(new DataCommand('#ajax_selected_color', 'form_state_value_select', $form_state->getValue('select')));
26 * Ajax callback triggered by date.
28 public function dateCallback($form, FormStateInterface $form_state) {
29 $response = new AjaxResponse();
30 $response->addCommand(new HtmlCommand('#ajax_date_value', $form_state->getValue('date')));
31 $response->addCommand(new DataCommand('#ajax_date_value', 'form_state_value_date', $form_state->getValue('date')));
36 * Ajax callback triggered by datetime.
38 public function datetimeCallback($form, FormStateInterface $form_state) {
39 $datetime = $form_state->getValue('datetime')['date'] . ' ' . $form_state->getValue('datetime')['time'];
41 $response = new AjaxResponse();
42 $response->addCommand(new HtmlCommand('#ajax_datetime_value', $datetime));
43 $response->addCommand(new DataCommand('#ajax_datetime_value', 'form_state_value_datetime', $datetime));
48 * Ajax callback triggered by checkbox.
50 public function checkboxCallback($form, FormStateInterface $form_state) {
51 $response = new AjaxResponse();
52 $response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state->getValue('checkbox')));
53 $response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
58 * Ajax callback to confirm image button was submitted.
60 public function imageButtonCallback($form, FormStateInterface $form_state) {
61 $response = new AjaxResponse();
62 $response->addCommand(new HtmlCommand('#ajax_image_button_result', "<div id='ajax-1-more-div'>Something witty!</div>"));
67 * Ajax callback triggered by the checkbox in a #group.
69 public function checkboxGroupCallback($form, FormStateInterface $form_state) {
70 return $form['checkbox_in_group_wrapper'];