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.
15 class AjaxCssForm extends FormBase {
20 public function getFormId() {
21 return 'ckeditor_test_ajax_css_form';
27 public function buildForm(array $form, FormStateInterface $form_state) {
28 // Create an inline and iframe CKEditor instance so we can test against
31 '#type' => 'container',
34 'ckeditor_test/ajax_css',
37 '#children' => $this->t('Here be dragons.'),
40 '#type' => 'text_format',
41 '#default_value' => $this->t('Here be llamas.'),
44 // A pair of buttons to trigger the AJAX events.
48 '#value' => $this->t('Add CSS to inline CKEditor instance'),
50 'callback' => [$this, 'addCssInline'],
55 '#value' => $this->t('Add CSS to iframe CKEditor instance'),
57 'callback' => [$this, 'addCssIframe'],
69 public function submitForm(array &$form, FormStateInterface $form_state) {
70 // Nothing to do here.
74 * Generates an AJAX response to add CSS to a CKEditor Text Editor instance.
76 * @param string $editor_id
77 * The Text Editor instance ID.
79 * @return \Drupal\Core\Ajax\AjaxResponse
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);
88 $response = new AjaxResponse();
90 ->addCommand(new AddStyleSheetCommand($editor_id, [$url]));
94 * Handles the AJAX request to add CSS to the inline editor.
96 * @return \Drupal\Core\Ajax\AjaxResponse
99 public function addCssInline() {
100 return $this->generateResponse('edit-inline');
104 * Handles the AJAX request to add CSS to the iframe editor.
106 * @return \Drupal\Core\Ajax\AjaxResponse
109 public function addCssIframe() {
110 return $this->generateResponse('edit-iframe-value');