You are here

public function VideoEmbedDialog::buildForm in Video Embed Field 8

Same name and namespace in other branches
  1. 8.2 modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php \Drupal\video_embed_wysiwyg\Form\VideoEmbedDialog::buildForm()

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

modules/video_embed_wysiwyg/src/Form/VideoEmbedDialog.php, line 64

Class

VideoEmbedDialog
A class for a video embed dialog.

Namespace

Drupal\video_embed_wysiwyg\Form

Code

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

  // Add AJAX support.
  $form['#prefix'] = '<div id="video-embed-dialog-form">';
  $form['#suffix'] = '</div>';

  // Ensure relevant dialog libraries are attached.
  $form['#attached']['library'][] = 'editor/drupal.editor.dialog';

  // Simple URL field and submit button for video URL.
  $form['video_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Video URL'),
    '#required' => TRUE,
    '#default_value' => $this
      ->getUserInput($form_state, 'video_url'),
  ];

  // If no settings are found, use the defaults configured in the filter
  // formats interface.
  $settings = $this
    ->getUserInput($form_state, 'settings');
  if (empty($settings) && ($editor = Editor::load($filter_format
    ->id()))) {
    $editor_settings = $editor
      ->getSettings();
    $plugin_settings = NestedArray::getValue($editor_settings, [
      'plugins',
      'video_embed',
      'defaults',
      'children',
    ]);
    $settings = $plugin_settings ? $plugin_settings : [];
  }

  // Create a settings form from the existing video formatter.
  $form['settings'] = Video::mockInstance($settings)
    ->settingsForm([], new FormState());
  $form['settings']['#type'] = 'fieldset';
  $form['settings']['#title'] = $this
    ->t('Settings');
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['save_modal'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#submit' => [],
    '#ajax' => [
      'callback' => '::ajaxSubmit',
      'event' => 'click',
      'wrapper' => 'video-embed-dialog-form',
    ],
  ];
  return $form;
}