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