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 $date = $form_state->getValue('date');
31 $response->addCommand(new HtmlCommand('#ajax_date_value', sprintf('<div>%s</div>', $date)));
32 $response->addCommand(new DataCommand('#ajax_date_value', 'form_state_value_date', $form_state->getValue('date')));
37 * Ajax callback triggered by datetime.
39 public function datetimeCallback($form, FormStateInterface $form_state) {
40 $datetime = $form_state->getValue('datetime')['date'] . ' ' . $form_state->getValue('datetime')['time'];
42 $response = new AjaxResponse();
43 $response->addCommand(new HtmlCommand('#ajax_datetime_value', sprintf('<div>%s</div>', $datetime)));
44 $response->addCommand(new DataCommand('#ajax_datetime_value', 'form_state_value_datetime', $datetime));
49 * Ajax callback triggered by checkbox.
51 public function checkboxCallback($form, FormStateInterface $form_state) {
52 $response = new AjaxResponse();
53 $response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state->getValue('checkbox')));
54 $response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
59 * Ajax callback to confirm image button was submitted.
61 public function imageButtonCallback($form, FormStateInterface $form_state) {
62 $response = new AjaxResponse();
63 $response->addCommand(new HtmlCommand('#ajax_image_button_result', "<div id='ajax-1-more-div'>Something witty!</div>"));
68 * Ajax callback triggered by the checkbox in a #group.
70 public function checkboxGroupCallback($form, FormStateInterface $form_state) {
71 return $form['checkbox_in_group_wrapper'];