Version 1
[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 class AjaxTestDialogForm extends FormBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function getFormId() {
21     return 'ajax_test_dialog_form';
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function buildForm(array $form, FormStateInterface $form_state) {
28     // In order to use WebTestBase::drupalPostAjaxForm() to POST from a link, we need
29     // to have a dummy field we can set in WebTestBase::drupalPostForm() else it won't
30     // submit anything.
31     $form['textfield'] = [
32       '#type' => 'hidden'
33     ];
34     $form['button1'] = [
35       '#type' => 'submit',
36       '#name' => 'button1',
37       '#value' => 'Button 1 (modal)',
38       '#ajax' => [
39         'callback' => '::modal',
40       ],
41     ];
42     $form['button2'] = [
43       '#type' => 'submit',
44       '#name' => 'button2',
45       '#value' => 'Button 2 (non-modal)',
46       '#ajax' => [
47         'callback' => '::nonModal',
48       ],
49     ];
50
51     return $form;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function validateForm(array &$form, FormStateInterface $form_state) {
58
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function submitForm(array &$form, FormStateInterface $form_state) {
65     $form_state->setRedirect('ajax_test.dialog_contents');
66   }
67
68
69   /**
70    * AJAX callback handler for AjaxTestDialogForm.
71    */
72   public function modal(&$form, FormStateInterface $form_state) {
73     return $this->dialog(TRUE);
74   }
75
76   /**
77    * AJAX callback handler for AjaxTestDialogForm.
78    */
79   public function nonModal(&$form, FormStateInterface $form_state) {
80     return $this->dialog(FALSE);
81   }
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 }