public function MailchimpSignupPageForm::submitForm in Mailchimp 2.x
Same name and namespace in other branches
- 8 modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php \Drupal\mailchimp_signup\Form\MailchimpSignupPageForm::submitForm()
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
- modules/
mailchimp_signup/ src/ Form/ MailchimpSignupPageForm.php, line 302
Class
- MailchimpSignupPageForm
- Subscribe to a Mailchimp list/audience.
Namespace
Drupal\mailchimp_signup\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
global $base_url;
$list_details = mailchimp_get_lists($this->signup->mc_lists);
$subscribe_lists = [];
// Filter out blank fields so we don't erase values on the Mailchimp side.
$mergevars = array_filter($form_state
->getValue('mergevars'));
$email = $mergevars['EMAIL'];
$gdpr_consent = $form_state
->getValue('gdpr_consent');
$mailchimp_lists = $form_state
->getValue('mailchimp_lists');
// If we only have one list we won't have checkbox values to investigate.
if (count(array_filter($this->signup->mc_lists)) == 1) {
$subscribe_lists[0] = [
'subscribe' => reset($this->signup->mc_lists),
'interest_groups' => isset($mailchimp_lists['interest_groups']) ? $mailchimp_lists['interest_groups'] : NULL,
];
}
else {
// We can look at the checkbox values now.
foreach ($mailchimp_lists as $list) {
if ($list['subscribe']) {
$subscribe_lists[] = $list;
}
}
}
$successes = [];
// Loop through the selected lists and try to subscribe.
foreach ($subscribe_lists as $list_choices) {
$list_id = $list_choices['subscribe'];
$interests = isset($list_choices['interest_groups']) ? $list_choices['interest_groups'] : [];
if (isset($this->signup->settings['safe_interest_groups']) && $this->signup->settings['safe_interest_groups']) {
$current_status = mailchimp_get_memberinfo($list_id, $email);
if (isset($current_status->interests)) {
$current_interests = [];
foreach ($current_status->interests as $id => $selected) {
if ($selected) {
$current_interests[$id] = $id;
}
}
$interests[] = $current_interests;
}
}
$result = mailchimp_subscribe($list_id, $email, $mergevars, $interests, $this->signup->settings['doublein'], 'html', NULL, $gdpr_consent);
if (empty($result)) {
$this->messenger
->addWarning($this
->t('There was a problem with your newsletter signup to %list.', [
'%list' => $list_details[$list_id]->name,
]));
}
else {
$successes[] = $list_details[$list_id]->name;
}
}
if (count($successes) && strlen($this->signup->settings['confirmation_message'])) {
$this->messenger
->addStatus($this
->t($this->signup->settings['confirmation_message']));
$form_state
->set('mailchimp_success', TRUE);
}
$destination = $this->signup->settings['destination'];
if (empty($destination)) {
$destination_url = Url::fromRoute('<current>');
}
else {
$destination_url = Url::fromUri($base_url . '/' . $this->signup->settings['destination']);
}
$form_state
->setRedirectUrl($destination_url);
}