You are here

public function AddMailchimpEvent::submitForm in Mailchimp 2.x

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_events/modules/mailchimp_events_example/src/Form/AddMailchimpEvent.php, line 121

Class

AddMailchimpEvent
A sample form for adding a Mailchimp Event.

Namespace

Drupal\mailchimp_events_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $email = $form_state
    ->getValue('email');
  $list = $form_state
    ->getValue('list');
  $properties = [
    'value' => $form_state
      ->getValue('event_value'),
    'another_property' => 'Always the same!',
  ];
  $event_name = $form_state
    ->getValue('event_name');
  $occurred_at = $form_state
    ->getValue('occurred_at')
    ->getTimestamp();
  $is_syncing = $form_state
    ->getValue('is_syncing');
  $result = mailchimp_events_add_member_event($list, $email, $event_name, $properties, $is_syncing, $occurred_at);
  $debug = $this
    ->t("Called function: mailchimp_events_add_member_event(%list, %email, %event_name, %properties, %is_syncing, %occurred_at).", [
    '%list' => $list,
    '%email' => $email,
    '%event_name' => $event_name,
    '%properties' => print_r($properties, TRUE),
    '%is_syncing' => $is_syncing,
    '%occurred_at' => $occurred_at,
  ]);
  if ($result !== FALSE) {
    $this
      ->messenger()
      ->addStatus($debug);
  }
  else {
    $this
      ->messenger()
      ->addError($debug);
    $this
      ->messenger()
      ->addError($this
      ->t('No results returned. Check the <a href=":watchdog">logs for Mailchimp</a>', [
      ':watchdog' => Url::fromRoute('dblog.overview')
        ->toString(),
    ]));
  }
}