Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / ajax_forms_test / src / Callbacks.php
1 <?php
2
3 namespace Drupal\ajax_forms_test;
4
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\DataCommand;
7 use Drupal\Core\Ajax\HtmlCommand;
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * Simple object for testing methods as Ajax callbacks.
12  */
13 class Callbacks {
14
15   /**
16    * Ajax callback triggered by select.
17    */
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')));
22     return $response;
23   }
24
25   /**
26    * Ajax callback triggered by date.
27    */
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')));
33     return $response;
34   }
35
36   /**
37    * Ajax callback triggered by datetime.
38    */
39   public function datetimeCallback($form, FormStateInterface $form_state) {
40     $datetime = $form_state->getValue('datetime')['date'] . ' ' . $form_state->getValue('datetime')['time'];
41
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));
45     return $response;
46   }
47
48   /**
49    * Ajax callback triggered by checkbox.
50    */
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')));
55     return $response;
56   }
57
58   /**
59    * Ajax callback to confirm image button was submitted.
60    */
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>"));
64     return $response;
65   }
66
67   /**
68    * Ajax callback triggered by the checkbox in a #group.
69    */
70   public function checkboxGroupCallback($form, FormStateInterface $form_state) {
71     return $form['checkbox_in_group_wrapper'];
72   }
73
74 }