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