diff --git a/modules/mailchimp_signup/config/schema/mailchimp_signup.schema.yml b/modules/mailchimp_signup/config/schema/mailchimp_signup.schema.yml index c8cf459..db64678 100644 --- a/modules/mailchimp_signup/config/schema/mailchimp_signup.schema.yml +++ b/modules/mailchimp_signup/config/schema/mailchimp_signup.schema.yml @@ -53,3 +53,6 @@ mailchimp_signup.mailchimp_signup.*: destination: type: string label: 'Destination' + ajax: + type: boolean + label: 'Ajax form submit' diff --git a/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php b/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php index 182cdd7..c9bf54b 100644 --- a/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php +++ b/modules/mailchimp_signup/src/Form/MailchimpSignupForm.php @@ -132,6 +132,13 @@ class MailchimpSignupForm extends EntityForm { '#default_value' => isset($signup->settings['destination']) ? $signup->settings['destination'] : NULL, ); + $form['settings']['ajax_submit'] = array( + '#type' => 'checkbox', + '#title' => t('AJAX Form Submit Mode'), + '#description' => t('Select if signup form submit should use AJAX instead of default page reload. Destination page will be ignored if checked.'), + '#default_value' => isset($signup->settings['ajax_submit']) ? $signup->settings['ajax_submit'] : FALSE, + ); + $form['mc_lists_config'] = array( '#type' => 'details', '#title' => t('MailChimp List Selection & Configuration'), diff --git a/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php b/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php index f034fa5..085e256 100644 --- a/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php +++ b/modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php @@ -5,6 +5,10 @@ namespace Drupal\mailchimp_signup\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Url; +use Drupal\Component\Utility\Html; +use Drupal\Core\Ajax\AjaxResponse; +use Drupal\Core\Ajax\HtmlCommand; +use Drupal\Core\Ajax\ReplaceCommand; use Drupal\mailchimp_signup\Entity\MailchimpSignup; /** @@ -61,6 +65,18 @@ class MailchimpSignupPageForm extends FormBase { '#markup' => $this->signup->description, ); + // If the form is configured to submit via AJAX + // add an empty container element, that is going + // to be replaced with the returned response + // instead of replacing the form entirely. + if ($this->signup->settings['ajax_submit']) { + $form['response'] = [ + '#prefix' => '
', + '#suffix' => '
', + ]; + } + + $form['mailchimp_lists'] = array('#tree' => TRUE); $lists = mailchimp_get_lists($this->signup->mc_lists); @@ -135,6 +151,18 @@ class MailchimpSignupPageForm extends FormBase { '#disabled' => (empty($lists)), ]; + // Add ajax submit to form if configured. + if ($this->signup->settings['ajax_submit']) { + $form_wrapper = Html::getId($this->formId); + $response_wrapper = "mailchimp-response-{$this->formId}-wrapper"; + // Get wrapper form id via cores way, see FormBuilder doBuildForm(). + $form['#prefix'] = '
'; + $form['#suffix'] = '
'; + $form['actions']['submit']['#ajax'] = [ + 'callback' => '::ajaxSubmit', + 'response_wrapper' => $response_wrapper, + ]; + } return $form; } @@ -247,4 +275,28 @@ class MailchimpSignupPageForm extends FormBase { $form_state->setRedirectUrl($destination_url); } + /** + * Ajax submit handler. + * + * @param array $form + * The form itself. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current form state. + * + * @return \Drupal\Core\Ajax\AjaxResponse + * A ajax response. + */ + 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)); + + return $response; + } + }