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.
18 class EditorLinkDialog extends FormBase {
23 public function getFormId() {
24 return 'editor_link_dialog';
30 * @param \Drupal\editor\Entity\Editor $editor
31 * The text editor to which this dialog corresponds.
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'] : [];
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>';
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'] : '',
56 $form['actions']['save_modal'] = [
58 '#value' => $this->t('Save'),
59 // No regular submit-handler. This form only works via JavaScript.
62 'callback' => '::submitForm',
73 public function submitForm(array &$form, FormStateInterface $form_state) {
74 $response = new AjaxResponse();
76 if ($form_state->getErrors()) {
77 unset($form['#prefix'], $form['#suffix']);
78 $form['status_messages'] = [
79 '#type' => 'status_messages',
82 $response->addCommand(new HtmlCommand('#editor-link-dialog-form', $form));
85 $response->addCommand(new EditorDialogSave($form_state->getValues()));
86 $response->addCommand(new CloseModalDialogCommand());