You are here

public function VideoFilterDialog::submitForm in Video Filter 8

Form submission handler.

Parameters

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

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

Overrides FormInterface::submitForm

File

src/Form/VideoFilterDialog.php, line 134

Class

VideoFilterDialog
Provides Video Filter dialog for text editors.

Namespace

Drupal\video_filter\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();

  // Generate shortcut/token code.
  $url = $form_state
    ->getValue('url');
  $supported = FALSE;

  // Check URL and validate is provider is supported.
  // If yes then generate a token with all the parameters from a dialog form.
  if ($url) {
    $shortcode = '[video:' . $url;
    $input = $form_state
      ->getUserInput();
    $vf = new VideoFilterCore();
    $plugin = $vf
      ->loadPlugins($url);
    if (!empty($plugin['options'])) {
      foreach ($input['options'][$plugin['id']]['options'] as $key => $val) {
        if (!empty($val)) {
          $shortcode .= ' ' . $key . ':' . $val;
        }
      }
      $shortcode .= ' provider:' . $plugin['id'];
      $supported = TRUE;
    }
    if ($form_state
      ->getValue('align') && $form_state
      ->getValue('align') != 'none') {
      $shortcode .= ' align:' . $form_state
        ->getValue('align');
    }
    $shortcode .= ']';
  }
  if ($supported && !empty($url)) {
    $form_state
      ->setValue([
      'attributes',
      'code',
    ], $shortcode);
  }
  if ($form_state
    ->getErrors()) {
    unset($form['#prefix'], $form['#suffix']);
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#video-filter-dialog-form', $form));
  }
  else {
    $response
      ->addCommand(new EditorDialogSave($form_state
      ->getValues()));
    $response
      ->addCommand(new CloseModalDialogCommand());
  }
  return $response;
}