Updated to Drupal 8.5. Core Media not yet in use.
[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   /**
72    * AJAX callback handler for AjaxTestDialogForm.
73    */
74   public function modal(&$form, FormStateInterface $form_state) {
75     return $this->dialog(TRUE);
76   }
77
78   /**
79    * AJAX callback handler for AjaxTestDialogForm.
80    */
81   public function nonModal(&$form, FormStateInterface $form_state) {
82     return $this->dialog(FALSE);
83   }
84
85
86   /**
87    * Util to render dialog in ajax callback.
88    *
89    * @param bool $is_modal
90    *   (optional) TRUE if modal, FALSE if plain dialog. Defaults to FALSE.
91    *
92    * @return \Drupal\Core\Ajax\AjaxResponse
93    *   An ajax response object.
94    */
95   protected function dialog($is_modal = FALSE) {
96     $content = AjaxTestController::dialogContents();
97     $response = new AjaxResponse();
98     $title = $this->t('AJAX Dialog & contents');
99
100     // Attach the library necessary for using the Open(Modal)DialogCommand and
101     // set the attachments for this Ajax response.
102     $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
103
104     if ($is_modal) {
105       $response->addCommand(new OpenModalDialogCommand($title, $content));
106     }
107     else {
108       $selector = '#ajax-test-dialog-wrapper-1';
109       $response->addCommand(new OpenDialogCommand($selector, $title, $content));
110     }
111     return $response;
112   }
113
114 }