You are here

public function MailchimpListsWebhookSettingsForm::submitForm in Mailchimp 2.x

Same name and namespace in other branches
  1. 8 modules/mailchimp_lists/src/Form/MailchimpListsWebhookSettingsForm.php \Drupal\mailchimp_lists\Form\MailchimpListsWebhookSettingsForm::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 ConfigFormBase::submitForm

File

modules/mailchimp_lists/src/Form/MailchimpListsWebhookSettingsForm.php, line 97

Class

MailchimpListsWebhookSettingsForm
Configure settings for a Mailchimp list webhook.

Namespace

Drupal\mailchimp_lists\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /* @var \Mailchimp\MailchimpLists $mc_lists */
  $mc_lists = mailchimp_get_api_object('MailchimpLists');
  $list = $form_state
    ->get('list');
  $webhook_events = $form_state
    ->getValue('webhook_events');
  $events = [];
  foreach ($webhook_events as $webhook_id => $enable) {
    $events[$webhook_id] = $enable === 1;
  }
  $result = FALSE;
  if (count($events) > 0) {
    $webhook_url = mailchimp_webhook_url();
    $webhooks = mailchimp_webhook_get($list->id);
    if (!empty($webhooks)) {
      foreach ($webhooks as $webhook) {
        if ($webhook->url == $webhook_url) {

          // Delete current webhook.
          mailchimp_webhook_delete($list->id, mailchimp_webhook_url());
        }
      }
    }
    $sources = [
      'user' => TRUE,
      'admin' => TRUE,
      'api' => FALSE,
    ];

    // Add webhook with enabled events.
    $result = mailchimp_webhook_add($list->id, mailchimp_webhook_url(), $events, $sources);
  }
  if ($result) {
    $this->messenger
      ->addStatus($this
      ->t('Webhooks for audience "%name" have been updated.', [
      '%name' => $list->name,
    ]));
  }
  else {
    $this->messenger
      ->addWarning($this
      ->t('Unable to update webhooks for audience "%name".', [
      '%name' => $list->name,
    ]));
  }
  $form_state
    ->setRedirect('mailchimp_lists.overview');
}