You are here

public function CKEditorEntityLinkDialog::buildForm in CKEditor Entity Link 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/CKEditorEntityLinkDialog.php, line 84

Class

CKEditorEntityLinkDialog
Provides a link dialog for text editors.

Namespace

Drupal\ckeditor_entity_link\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, FilterFormat $filter_format = NULL) {
  $config = $this
    ->config('ckeditor_entity_link.settings');

  // The default values are set directly from \Drupal::request()->request,
  // provided by the editor plugin opening the dialog.
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
  $form['#prefix'] = '<div id="ckeditor-entity-link-dialog-form">';
  $form['#suffix'] = '</div>';
  $entity_types = $this->entityTypeManager
    ->getDefinitions();
  $types = [];
  foreach ($config
    ->get('entity_types') as $type => $selected) {
    if ($selected) {
      $types[$type] = $entity_types[$type]
        ->getLabel();
    }
  }
  $form['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Link type'),
    '#options' => $types,
    '#default_value' => 'node',
    '#required' => TRUE,
    '#size' => 1,
    '#ajax' => [
      'callback' => '::updateTypeSettings',
      'effect' => 'fade',
    ],
  ];
  $entity_type = empty($form_state
    ->getValue('entity_type')) ? 'node' : $form_state
    ->getValue('entity_type');
  $bundles = [];
  foreach ($config
    ->get($entity_type . '_bundles') as $bundle => $selected) {
    if ($selected) {
      $bundles[] = $bundle;
    }
  }
  $form['entity_id'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => $entity_type,
    '#title' => $this
      ->t('Link'),
    '#required' => TRUE,
    '#prefix' => '<div id="entity-id-wrapper">',
    '#suffix' => '</div>',
  ];
  if (!empty($bundles)) {
    $form['entity_id']['#selection_settings']['target_bundles'] = $bundles;
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save_modal'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    // No regular submit-handler. This form only works via JavaScript.
    '#submit' => [],
    '#ajax' => [
      'callback' => '::submitForm',
      'event' => 'click',
    ],
  ];
  return $form;
}