You are here

public function MailchimpSignupForm::mergefields_callback in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_signup/src/Form/MailchimpSignupForm.php \Drupal\mailchimp_signup\Form\MailchimpSignupForm::mergefields_callback()

AJAX callback handler for MailchimpSignupForm.

File

modules/mailchimp_signup/src/Form/MailchimpSignupForm.php, line 383

Class

MailchimpSignupForm
Form controller for the MailchimpSignup entity edit form.

Namespace

Drupal\mailchimp_signup\Form

Code

public function mergefields_callback(&$form, FormStateInterface $form_state) {

  // We can return either a form element here (default functionality) or
  // an instance of the AjaxResponse with the appropriate commands
  // to be executed.
  $response = new AjaxResponse();

  // This one acts as a replacement for the original return value that
  // was defined here beforehand. Simply replace the merge fields
  // for each of the selected mailing lists.
  $response
    ->addCommand(new HtmlCommand('#mergefields-wrapper', $form['mc_lists_config']['mergefields']));

  // This one here is coupled to the functionality for the interest groups.
  // It's required because of the following scenarion:
  // The Ajax callback mapped to the "configure_groups" checkbox is triggered
  // once the checkbox is selected, thus loading the interest groups for all
  // of the selected mailing lists. But in case a user tries to select a
  // mailing list afterwards, the callback will no longer execute. So we
  // can add a new command to replace the list in that container.
  if ($form_state
    ->getValue('configure_groups')) {
    $response
      ->addCommand(new HtmlCommand('#interest-groups-container', $form['subscription_settings']['groups_container']));
  }
  return $response;
}