You are here

public function ListMailchimpEventsForMember::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/ListMailchimpEventsForMember.php, line 24

Class

ListMailchimpEventsForMember
A sample form for listing Mailchimp Events for a member.

Namespace

Drupal\mailchimp_events_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['description']['#markup'] = $this
    ->t('View a list of events for a specific audience member, directly pulled from Mailchimp.');
  $mc_lists = mailchimp_get_lists();
  $list_options = [];
  if ($events = $form_state
    ->get('events')) {
    $listing = [];
    foreach ($events->events as $event) {
      $properties = [];
      if (isset($event->properties)) {
        foreach ($event->properties as $key => $value) {
          $properties[] = $key . ': ' . $value;
        }
      }
      $listing[] = [
        '#prefix' => isset($event->name) ? $event->name : '',
        '#theme' => 'item_list',
        '#items' => $properties,
      ];
    }
    $form['results'] = [
      '#theme' => 'item_list',
      '#items' => $listing,
    ];
  }
  foreach ($mc_lists as $key => $value) {
    $list_options[$key] = $value->name;
  }
  $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 of the member to view events for.'),
    '#weight' => '0',
  ];
  $form['count'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Count'),
    '#description' => $this
      ->t('The number of records to return. Default value is 10. Maximum value is 1000.'),
    '#default_value' => 10,
    '#max' => 1000,
  ];
  $form['offset'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Offset'),
    '#description' => $this
      ->t('Used for pagination, this it the number of records from a collection to skip. Default value is 0.'),
    '#default_value' => 0,
  ];
  $form['fields'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Fields'),
    '#description' => $this
      ->t('A comma-separated list of fields to return. Reference parameters of sub-objects with dot notation. If left empty, events.name, events.properties, and events.occurred_at will be returned.'),
    '#default_value' => 'events.name,events.properties,events.occurred_at',
  ];
  $form['exclude_fields'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Exclude fields'),
    '#description' => $this
      ->t('A comma-separated list of fields to exclude. Reference parameters of sub-objects with dot notation. The options available match the options in "Fields".'),
    '#default_value' => '',
  ];
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Submit'),
  ];
  return $form;
}