3 namespace Drupal\editor\Form;
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;
14 * Provides a link dialog for text editors.
16 class EditorLinkDialog extends FormBase {
21 public function getFormId() {
22 return 'editor_link_dialog';
28 * @param \Drupal\editor\Entity\Editor $editor
29 * The text editor to which this dialog corresponds.
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'] : [];
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>';
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'] : '',
54 $form['actions']['save_modal'] = [
56 '#value' => $this->t('Save'),
57 // No regular submit-handler. This form only works via JavaScript.
60 'callback' => '::submitForm',
71 public function submitForm(array &$form, FormStateInterface $form_state) {
72 $response = new AjaxResponse();
74 if ($form_state->getErrors()) {
75 unset($form['#prefix'], $form['#suffix']);
76 $form['status_messages'] = [
77 '#type' => 'status_messages',
80 $response->addCommand(new HtmlCommand('#editor-link-dialog-form', $form));
83 $response->addCommand(new EditorDialogSave($form_state->getValues()));
84 $response->addCommand(new CloseModalDialogCommand());