Version 1
[yaffs-website] / web / core / modules / ckeditor / tests / modules / src / Form / AjaxCssForm.php
1 <?php
2
3 namespace Drupal\ckeditor_test\Form;
4
5 use Drupal\ckeditor\Ajax\AddStyleSheetCommand;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9
10 /**
11  * A form for testing delivery of CSS to CKEditor via AJAX.
12  */
13 class AjaxCssForm extends FormBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public function getFormId() {
19     return 'ckeditor_test_ajax_css_form';
20   }
21
22   /**
23    * {@inheritdoc}
24    */
25   public function buildForm(array $form, FormStateInterface $form_state) {
26     // Create an inline and iframe CKEditor instance so we can test against
27     // both.
28     $form['inline'] = [
29       '#type' => 'container',
30       '#attached' => [
31         'library' => [
32           'ckeditor_test/ajax_css',
33         ],
34       ],
35       '#children' => $this->t('Here be dragons.'),
36     ];
37     $form['iframe'] = [
38       '#type' => 'text_format',
39       '#default_value' => $this->t('Here be llamas.'),
40     ];
41
42     // A pair of buttons to trigger the AJAX events.
43     $form['actions'] = [
44       'css_inline' => [
45         '#type' => 'submit',
46         '#value' => $this->t('Add CSS to inline CKEditor instance'),
47         '#ajax' => [
48           'callback' => [$this, 'addCssInline'],
49         ],
50       ],
51       'css_frame' => [
52         '#type' => 'submit',
53         '#value' => $this->t('Add CSS to iframe CKEditor instance'),
54         '#ajax' => [
55           'callback' => [$this, 'addCssIframe'],
56         ],
57       ],
58       '#type' => 'actions',
59     ];
60
61     return $form;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function submitForm(array &$form, FormStateInterface $form_state) {
68     // Nothing to do here.
69   }
70
71   /**
72    * Generates an AJAX response to add CSS to a CKEditor Text Editor instance.
73    *
74    * @param string $editor_id
75    *   The Text Editor instance ID.
76    *
77    * @return \Drupal\Core\Ajax\AjaxResponse
78    *   An AJAX response.
79    */
80   protected function generateResponse($editor_id) {
81     // Build a URL to the style sheet that will be added.
82     $url = drupal_get_path('module', 'ckeditor_test') . '/css/test.css';
83     $url = file_create_url($url);
84     $url = file_url_transform_relative($url);
85
86     $response = new AjaxResponse();
87     return $response
88       ->addCommand(new AddStyleSheetCommand($editor_id, [$url]));
89   }
90
91   /**
92    * Handles the AJAX request to add CSS to the inline editor.
93    *
94    * @return \Drupal\Core\Ajax\AjaxResponse
95    *   An AJAX response.
96    */
97   public function addCssInline() {
98     return $this->generateResponse('edit-inline');
99   }
100
101   /**
102    * Handles the AJAX request to add CSS to the iframe editor.
103    *
104    * @return \Drupal\Core\Ajax\AjaxResponse
105    *   An AJAX response.
106    */
107   public function addCssIframe() {
108     return $this->generateResponse('edit-iframe-value');
109   }
110
111 }