You are here

public function MailchimpSignupPageForm::ajaxSubmit in Mailchimp 2.x

Same name and namespace in other branches
  1. 8 modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php \Drupal\mailchimp_signup\Form\MailchimpSignupPageForm::ajaxSubmit()

Ajax submit handler.

Parameters

array $form: The form itself.

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

Return value

\Drupal\Core\Ajax\AjaxResponse A ajax response.

File

modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php, line 392

Class

MailchimpSignupPageForm
Subscribe to a Mailchimp list/audience.

Namespace

Drupal\mailchimp_signup\Form

Code

public function ajaxSubmit(array $form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $response_wrapper_id = '#' . $form['actions']['submit']['#ajax']['response_wrapper'];

  // Simply return any status messages as a content the the response wrapper.
  $status_messages = [
    '#type' => 'status_messages',
  ];
  $content = \Drupal::service('renderer')
    ->renderRoot($status_messages);
  $response
    ->addCommand(new HtmlCommand($response_wrapper_id, $content));

  // Also add an extra class to the form itself to signify the form changed.
  $form_wrapper_id = '#' . Html::getId($this->formId);
  $response
    ->addCommand(new ChangedCommand($form_wrapper_id));

  // Reset form values if submission was successful.
  if ($form_state
    ->get('mailchimp_success')) {
    $response
      ->addCommand(new InvokeCommand("{$form_wrapper_id} form,{$form_wrapper_id}", 'trigger', [
      'reset',
    ]));
  }
  return $response;
}