You are here

public function WebformMailChimpHandler::buildConfigurationForm in Webform Mailchimp 8.5

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides WebformHandlerBase::buildConfigurationForm

File

src/Plugin/WebformHandler/WebformMailChimpHandler.php, line 99

Class

WebformMailChimpHandler
Form submission to MailChimp handler.

Namespace

Drupal\webform_mailchimp\Plugin\WebformHandler

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $lists = mailchimp_get_lists();
  $options = [];
  foreach ($lists as $list) {
    $options[$list->id] = $list->name;
  }
  $form['mailchimp'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('MailChimp settings'),
    '#attributes' => [
      'id' => 'webform-mailchimp-handler-settings',
    ],
  ];
  $form['mailchimp']['update'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Refresh lists & groups'),
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxMailchimpListHandler',
      ],
      'wrapper' => 'webform-mailchimp-handler-settings',
    ],
    '#submit' => [
      [
        get_class($this),
        'maichimpUpdateConfigSubmit',
      ],
    ],
  ];
  $form['mailchimp']['list'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('List'),
    '#required' => TRUE,
    '#empty_option' => $this
      ->t('- Select an option -'),
    '#default_value' => $this->configuration['list'],
    '#options' => $options,
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxMailchimpListHandler',
      ],
      'wrapper' => 'webform-mailchimp-handler-settings',
    ],
    '#description' => $this
      ->t('Select the list you want to send this submission to. Alternatively, you can also use the Other field for token replacement.'),
  ];
  $fields = $this
    ->getWebform()
    ->getElementsInitializedAndFlattened();
  $options = [];
  foreach ($fields as $field_name => $field) {
    if (in_array($field['#type'], [
      'email',
      'webform_email_confirm',
    ])) {
      $options[$field_name] = $field['#title'];
    }
  }
  $default_value = $this->configuration['email'];
  if (empty($this->configuration['email']) && count($options) == 1) {
    $default_value = key($options);
  }
  $form['mailchimp']['email'] = [
    '#type' => 'webform_select_other',
    '#title' => $this
      ->t('Email field'),
    '#required' => TRUE,
    '#default_value' => $default_value,
    '#options' => $options,
    '#empty_option' => $this
      ->t('- Select an option -'),
    '#description' => $this
      ->t('Select the email element you want to use for subscribing to the mailchimp list specified above. Alternatively, you can also use the Other field for token replacement.'),
  ];
  $options = [];
  foreach ($fields as $field_name => $field) {
    if (in_array($field['#type'], [
      'checkbox',
      'webform_toggle',
    ])) {
      $options[$field_name] = $field['#title'];
    }
  }
  $form['mailchimp']['control'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Control field'),
    '#empty_option' => $this
      ->t('- Select an option -'),
    '#default_value' => $this->configuration['control'],
    '#options' => $options,
    '#description' => $this
      ->t('DEPRECATED: Use Webform\'s core conditions tab instead.'),
  ];
  $form['mailchimp']['mergevars'] = [
    '#type' => 'webform_codemirror',
    '#mode' => 'yaml',
    '#title' => $this
      ->t('Merge vars'),
    '#default_value' => $this->configuration['mergevars'],
    '#description' => $this
      ->t('You can map additional fields from your webform to fields in your MailChimp list, one per line. An example might be FNAME: [webform_submission:values:first_name]. You may use tokens.'),
  ];
  $form['mailchimp']['interest_groups'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Interest groups'),
    '#description' => $this
      ->t('Displays interest groups for the selected list. Visit <a href="@url">Getting Started with Groups</a> for more information.', [
      '@url' => 'https://kb.mailchimp.com/lists/groups/getting-started-with-groups',
    ]),
  ];

  // Get selected interest group. Fallback to the saved one.
  $list_id = $form_state
    ->getValue([
    'mailchimp',
    'list',
  ], $this->configuration['list']);
  if ($list_id) {
    $list = mailchimp_get_list($list_id);
    if (!empty($list->intgroups)) {
      $groups_default = $this->configuration['interest_groups'];
      if (empty($groups_default)) {
        $groups_default = [];
      }
      $form['mailchimp']['interest_groups'] += mailchimp_interest_groups_form_elements($list, $groups_default, NULL, 'admin');
    }
  }
  $form['mailchimp']['double_optin'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Double opt-in'),
    '#default_value' => $this->configuration['double_optin'],
  ];
  $form['mailchimp']['token_tree_link'] = $this->tokenManager
    ->buildTreeLink();
  return $form;
}