You are here

public function AddMailchimpEvent::buildForm in Mailchimp 2.x

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

modules/mailchimp_events/modules/mailchimp_events_example/src/Form/AddMailchimpEvent.php, line 28

Class

AddMailchimpEvent
A sample form for adding a Mailchimp Event.

Namespace

Drupal\mailchimp_events_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['description']['#markup'] = $this
    ->t('Cause an event to occur now, for a specific audience member in Mailchimp.');
  $add_link = Link::createFromRoute('You can add event types here.', 'entity.mailchimp_event.add_form', [], [
    'attributes' => [
      'target' => '_blank',
    ],
  ]);
  $mc_lists = mailchimp_get_lists();
  $list_options = [];
  foreach ($mc_lists as $key => $value) {
    $list_options[$key] = $value->name;
  }
  $events = MailchimpEvent::loadMultiple();
  $event_options = [];
  if (empty($events)) {
    $this
      ->messenger()
      ->addError($this
      ->t('At least one event type is required. @add-link', [
      '@add-link' => $add_link
        ->toString(),
    ]));
    return $form;
  }
  else {
    foreach ($events as $key => $event) {
      $event_options[$event
        ->getName()] = $event
        ->getName();
    }
  }
  $form['list'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Mailchimp audience'),
    '#weight' => '0',
    '#required' => TRUE,
    '#options' => $list_options,
  ];
  $form['email'] = [
    '#type' => 'email',
    '#title' => $this
      ->t('Email'),
    '#description' => $this
      ->t('The email address to associate with this event.'),
    '#weight' => '0',
    '#required' => TRUE,
  ];
  $form['event_name'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Event Name'),
    '#description' => $this
      ->t('The name of the Event. %add_link', [
      '%add_link' => $add_link
        ->toString(),
    ]),
    '#weight' => '0',
    '#required' => TRUE,
    '#options' => $event_options,
  ];
  $form['event_value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Event value'),
    '#description' => $this
      ->t('This can be any string.'),
    '#maxlength' => 64,
    '#size' => 64,
    '#weight' => '0',
    '#required' => TRUE,
  ];
  $form['is_syncing'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Is Syncing?'),
    '#description' => $this
      ->t('Events created with the is_syncing value set to true will not trigger automations.'),
  ];
  $form['occurred_at'] = [
    '#type' => 'datetime',
    '#description' => $this
      ->t('The date and time the event occurred. Defaults to the time when the form loaded.'),
    '#title' => $this
      ->t('Occurred at'),
    '#size' => 20,
    '#date_time_format' => 'H:i',
    '#default_value' => DrupalDateTime::createFromTimestamp(time()),
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}