5f34e1078695241bba24c50e9a390a146970f177
[yaffs-website] / web / core / modules / editor / src / Form / EditorLinkDialog.php
1 <?php
2
3 namespace Drupal\editor\Form;
4
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\editor\Entity\Editor;
8 use Drupal\Core\Ajax\AjaxResponse;
9 use Drupal\Core\Ajax\HtmlCommand;
10 use Drupal\editor\Ajax\EditorDialogSave;
11 use Drupal\Core\Ajax\CloseModalDialogCommand;
12
13 /**
14  * Provides a link dialog for text editors.
15  *
16  * @internal
17  */
18 class EditorLinkDialog extends FormBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getFormId() {
24     return 'editor_link_dialog';
25   }
26
27   /**
28    * {@inheritdoc}
29    *
30    * @param \Drupal\editor\Entity\Editor $editor
31    *   The text editor to which this dialog corresponds.
32    */
33   public function buildForm(array $form, FormStateInterface $form_state, Editor $editor = NULL) {
34     // The default values are set directly from \Drupal::request()->request,
35     // provided by the editor plugin opening the dialog.
36     $user_input = $form_state->getUserInput();
37     $input = isset($user_input['editor_object']) ? $user_input['editor_object'] : [];
38
39     $form['#tree'] = TRUE;
40     $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
41     $form['#prefix'] = '<div id="editor-link-dialog-form">';
42     $form['#suffix'] = '</div>';
43
44     // Everything under the "attributes" key is merged directly into the
45     // generated link tag's attributes.
46     $form['attributes']['href'] = [
47       '#title' => $this->t('URL'),
48       '#type' => 'textfield',
49       '#default_value' => isset($input['href']) ? $input['href'] : '',
50       '#maxlength' => 2048,
51     ];
52
53     $form['actions'] = [
54       '#type' => 'actions',
55     ];
56     $form['actions']['save_modal'] = [
57       '#type' => 'submit',
58       '#value' => $this->t('Save'),
59       // No regular submit-handler. This form only works via JavaScript.
60       '#submit' => [],
61       '#ajax' => [
62         'callback' => '::submitForm',
63         'event' => 'click',
64       ],
65     ];
66
67     return $form;
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function submitForm(array &$form, FormStateInterface $form_state) {
74     $response = new AjaxResponse();
75
76     if ($form_state->getErrors()) {
77       unset($form['#prefix'], $form['#suffix']);
78       $form['status_messages'] = [
79         '#type' => 'status_messages',
80         '#weight' => -10,
81       ];
82       $response->addCommand(new HtmlCommand('#editor-link-dialog-form', $form));
83     }
84     else {
85       $response->addCommand(new EditorDialogSave($form_state->getValues()));
86       $response->addCommand(new CloseModalDialogCommand());
87     }
88
89     return $response;
90   }
91
92 }