You are here

public function EntityEmbedDialog::buildForm in Entity Embed 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.

\Drupal\editor\EditorInterface $editor: The editor to which this dialog corresponds.

\Drupal\embed\EmbedButtonInterface $embed_button: The URL button to which this dialog corresponds.

Overrides FormInterface::buildForm

File

src/Form/EntityEmbedDialog.php, line 172

Class

EntityEmbedDialog
Provides a form to embed entities by specifying data attributes.

Namespace

Drupal\entity_embed\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, EditorInterface $editor = NULL, EmbedButtonInterface $embed_button = NULL) {
  $values = $form_state
    ->getValues();
  $input = $form_state
    ->getUserInput();

  // Set embed button element in form state, so that it can be used later in
  // validateForm() function.
  $form_state
    ->set('embed_button', $embed_button);
  $form_state
    ->set('editor', $editor);

  // Initialize entity element with form attributes, if present.
  $entity_element = empty($values['attributes']) ? [] : $values['attributes'];
  $entity_element += empty($input['attributes']) ? [] : $input['attributes'];

  // The default values are set directly from \Drupal::request()->request,
  // provided by the editor plugin opening the dialog.
  if (!$form_state
    ->get('entity_element')) {
    $form_state
      ->set('entity_element', isset($input['editor_object']) ? $input['editor_object'] : []);
  }
  $entity_element += $form_state
    ->get('entity_element');
  $entity_element += [
    'data-entity-type' => $embed_button
      ->getTypeSetting('entity_type'),
    'data-entity-uuid' => '',
    'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view',
    'data-entity-embed-display-settings' => isset($form_state
      ->get('entity_element')['data-entity-embed-settings']) ? $form_state
      ->get('entity_element')['data-entity-embed-settings'] : [],
  ];
  $form_state
    ->set('entity_element', $entity_element);
  $entity = $this
    ->loadEntityByAttributes($entity_element);
  $form_state
    ->set('entity', $entity ?: NULL);
  if (!$form_state
    ->get('step')) {

    // If an entity has been selected, then always skip to the embed options.
    if ($form_state
      ->get('entity')) {
      $form_state
        ->set('step', 'embed');
    }
    else {
      $form_state
        ->set('step', 'select');
    }
  }
  $form['#tree'] = TRUE;
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';
  $form['#attached']['library'][] = 'entity_embed/drupal.entity_embed.dialog';
  $form['#prefix'] = '<div id="entity-embed-dialog-form">';
  $form['#suffix'] = '</div>';
  $form['#attributes']['class'][] = 'entity-embed-dialog-step--' . $form_state
    ->get('step');
  $this
    ->loadEntityBrowser($form_state);
  if ($form_state
    ->get('step') == 'select') {
    $form = $this
      ->buildSelectStep($form, $form_state);
  }
  elseif ($form_state
    ->get('step') == 'review') {
    $form = $this
      ->buildReviewStep($form, $form_state);
  }
  elseif ($form_state
    ->get('step') == 'embed') {
    $form = $this
      ->buildEmbedStep($form, $form_state);
  }
  return $form;
}