You are here

public function HubspotDialog::submitForm in Hubspot forms 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/HubspotDialog.php, line 87

Class

HubspotDialog
Provides Hubspot Forms dialog for text editors.

Namespace

Drupal\hubspot_forms\Form

Code

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

  // Generate shortcut/token code.
  $form_empty = TRUE;
  $shortcode = '[hubspot-form:';
  if ($form_state
    ->getValue('formid')) {
    list($portal_id, $form_id) = explode("::", $form_state
      ->getValue('formid'));
    $shortcode .= $form_id;
    $form_empty = FALSE;
  }
  if (isset($portal_id)) {
    $shortcode .= ' portal_id:' . $portal_id;
  }
  $shortcode .= ']';
  if (!empty($shortcode) && !$form_empty) {
    $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('#hubspot-forms-dialog-form', $form));
  }
  else {
    $response
      ->addCommand(new EditorDialogSave($form_state
      ->getValues()));
    $response
      ->addCommand(new CloseModalDialogCommand());
  }
  return $response;
}