You are here

public function LinkitEditorDialog::buildForm in Linkit 8.4

Parameters

\Drupal\filter\Entity\FilterFormat $filter_format: The filter format for which this dialog corresponds.

Overrides FormInterface::buildForm

File

src/Form/LinkitEditorDialog.php, line 83
Contains \Drupal\linkit\Form\LinkitEditorDialog.

Class

LinkitEditorDialog
Provides a linkit dialog for text editors.

Namespace

Drupal\linkit\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {

  // The default values are set directly from \Drupal::request()->request,
  // provided by the editor plugin opening the dialog.
  $user_input = $form_state
    ->getUserInput();
  $input = isset($user_input['editor_object']) ? $user_input['editor_object'] : [];

  /** @var \Drupal\editor\EditorInterface $editor */
  $editor = $this->editorStorage
    ->load($filter_format
    ->id());
  $linkit_profile_id = $editor
    ->getSettings()['plugins']['linkit']['linkit_profile'];
  $this->linkitProfile = $this->linkitProfileStorage
    ->load($linkit_profile_id);
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
  $form['#prefix'] = '<div id="linkit-editor-dialog-form">';
  $form['#suffix'] = '</div>';

  // Everything under the "attributes" key is merged directly into the
  // generated link tag's attributes.
  $form['attributes']['href'] = [
    '#title' => $this
      ->t('Link'),
    '#type' => 'linkit',
    '#default_value' => isset($input['href']) ? $input['href'] : '',
    '#description' => $this
      ->t('Start typing to find content or paste a URL.'),
    '#autocomplete_route_name' => 'linkit.autocomplete',
    '#autocomplete_route_parameters' => [
      'linkit_profile_id' => $linkit_profile_id,
    ],
    '#weight' => 0,
  ];
  $this
    ->addAttributes($form, $form_state, $this->linkitProfile
    ->getAttributes(), $input);
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save_modal'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#submit' => [],
    '#ajax' => [
      'callback' => '::submitForm',
      'event' => 'click',
    ],
  ];
  return $form;
}