3 namespace Drupal\ckeditor_test\Form;
5 use Drupal\ckeditor\Ajax\AddStyleSheetCommand;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
11 * A form for testing delivery of CSS to CKEditor via AJAX.
13 class AjaxCssForm extends FormBase {
18 public function getFormId() {
19 return 'ckeditor_test_ajax_css_form';
25 public function buildForm(array $form, FormStateInterface $form_state) {
26 // Create an inline and iframe CKEditor instance so we can test against
29 '#type' => 'container',
32 'ckeditor_test/ajax_css',
35 '#children' => $this->t('Here be dragons.'),
38 '#type' => 'text_format',
39 '#default_value' => $this->t('Here be llamas.'),
42 // A pair of buttons to trigger the AJAX events.
46 '#value' => $this->t('Add CSS to inline CKEditor instance'),
48 'callback' => [$this, 'addCssInline'],
53 '#value' => $this->t('Add CSS to iframe CKEditor instance'),
55 'callback' => [$this, 'addCssIframe'],
67 public function submitForm(array &$form, FormStateInterface $form_state) {
68 // Nothing to do here.
72 * Generates an AJAX response to add CSS to a CKEditor Text Editor instance.
74 * @param string $editor_id
75 * The Text Editor instance ID.
77 * @return \Drupal\Core\Ajax\AjaxResponse
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);
86 $response = new AjaxResponse();
88 ->addCommand(new AddStyleSheetCommand($editor_id, [$url]));
92 * Handles the AJAX request to add CSS to the inline editor.
94 * @return \Drupal\Core\Ajax\AjaxResponse
97 public function addCssInline() {
98 return $this->generateResponse('edit-inline');
102 * Handles the AJAX request to add CSS to the iframe editor.
104 * @return \Drupal\Core\Ajax\AjaxResponse
107 public function addCssIframe() {
108 return $this->generateResponse('edit-iframe-value');