Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / modules / ajax_test / src / Form / AjaxTestDialogForm.php
1 <?php
2
3 namespace Drupal\ajax_test\Form;
4
5 use Drupal\ajax_test\Controller\AjaxTestController;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Ajax\AjaxResponse;
8 use Drupal\Core\Ajax\OpenModalDialogCommand;
9 use Drupal\Core\Ajax\OpenDialogCommand;
10 use Drupal\Core\Form\FormStateInterface;
11
12 /**
13  * Dummy form for testing DialogRenderer with _form routes.
14  *
15  * @internal
16  */
17 class AjaxTestDialogForm extends FormBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public function getFormId() {
23     return 'ajax_test_dialog_form';
24   }
25
26   /**
27    * {@inheritdoc}
28    */
29   public function buildForm(array $form, FormStateInterface $form_state) {
30     // In order to use WebTestBase::drupalPostAjaxForm() to POST from a link, we need
31     // to have a dummy field we can set in WebTestBase::drupalPostForm() else it won't
32     // submit anything.
33     $form['textfield'] = [
34       '#type' => 'hidden',
35     ];
36     $form['button1'] = [
37       '#type' => 'submit',
38       '#name' => 'button1',
39       '#value' => 'Button 1 (modal)',
40       '#ajax' => [
41         'callback' => '::modal',
42       ],
43     ];
44     $form['button2'] = [
45       '#type' => 'submit',
46       '#name' => 'button2',
47       '#value' => 'Button 2 (non-modal)',
48       '#ajax' => [
49         'callback' => '::nonModal',
50       ],
51     ];
52
53     return $form;
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function validateForm(array &$form, FormStateInterface $form_state) {
60
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function submitForm(array &$form, FormStateInterface $form_state) {
67     $form_state->setRedirect('ajax_test.dialog_contents');
68   }
69
70   /**
71    * AJAX callback handler for AjaxTestDialogForm.
72    */
73   public function modal(&$form, FormStateInterface $form_state) {
74     return $this->dialog(TRUE);
75   }
76
77   /**
78    * AJAX callback handler for AjaxTestDialogForm.
79    */
80   public function nonModal(&$form, FormStateInterface $form_state) {
81     return $this->dialog(FALSE);
82   }
83
84   /**
85    * Util to render dialog in ajax callback.
86    *
87    * @param bool $is_modal
88    *   (optional) TRUE if modal, FALSE if plain dialog. Defaults to FALSE.
89    *
90    * @return \Drupal\Core\Ajax\AjaxResponse
91    *   An ajax response object.
92    */
93   protected function dialog($is_modal = FALSE) {
94     $content = AjaxTestController::dialogContents();
95     $response = new AjaxResponse();
96     $title = $this->t('AJAX Dialog & contents');
97
98     // Attach the library necessary for using the Open(Modal)DialogCommand and
99     // set the attachments for this Ajax response.
100     $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
101
102     if ($is_modal) {
103       $response->addCommand(new OpenModalDialogCommand($title, $content));
104     }
105     else {
106       $selector = '#ajax-test-dialog-wrapper-1';
107       $response->addCommand(new OpenDialogCommand($selector, $title, $content));
108     }
109     return $response;
110   }
111
112 }