You are here

function mailchimp_interest_groups_form_elements in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_interest_groups_form_elements()
  2. 7.3 mailchimp.module \mailchimp_interest_groups_form_elements()
  3. 7.4 mailchimp.module \mailchimp_interest_groups_form_elements()
  4. 2.x mailchimp.module \mailchimp_interest_groups_form_elements()

Helper function to generate form elements for a list's interest groups.

Parameters

object $list: Mailchimp list object as returned by mailchimp_get_list().

array $defaults: Array of default values to use if no group subscription values already exist at Mailchimp.

string $email: Optional email address to pass to the MCAPI and retrieve existing values for use as defaults.

string $mode: Elements display mode:

  • "default" shows all groups except the hidden ones,
  • "admin" shows all groups including hidden ones,
  • "hidden" generates '#type' => 'value' elements using default values.

Return value

array A collection of form elements, one per interest group.

4 calls to mailchimp_interest_groups_form_elements()
MailchimpListsSelectWidget::formElement in modules/mailchimp_lists/src/Plugin/Field/FieldWidget/MailchimpListsSelectWidget.php
Returns the form for a single field widget.
MailchimpListsSubscribeForm::buildForm in modules/mailchimp_lists/src/Form/MailchimpListsSubscribeForm.php
Form constructor.
MailchimpSignupForm::form in modules/mailchimp_signup/src/Form/MailchimpSignupForm.php
Gets the actual form array to be built.
MailchimpSignupPageForm::buildForm in modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php
Form constructor.

File

./mailchimp.module, line 1186
Mailchimp module.

Code

function mailchimp_interest_groups_form_elements($list, array $defaults = [], $email = NULL, $mode = 'default') {
  $return = [];
  if ($mode == 'hidden') {
    foreach ($list->intgroups as $group) {
      $return[$group->id] = [
        '#type' => 'value',
        '#value' => isset($defaults[$group->id]) ? $defaults[$group->id] : [],
      ];
    }
    return $return;
  }
  try {
    foreach ($list->intgroups as $group) {

      /* @var \Mailchimp\MailchimpLists $mc_lists */
      $mc_lists = mailchimp_get_api_object('MailchimpLists');
      $interest_data = $mc_lists
        ->getInterests($list->id, $group->id, [
        'count' => 500,
      ]);
      if (!empty($email)) {
        $memberinfo = mailchimp_get_memberinfo($list->id, $email);
      }

      // phpcs:disable
      $field_title = t($group->title);

      // phpcs:enable
      $field_description = NULL;
      $value = NULL;

      // Set the form field type:
      switch ($group->type) {
        case 'hidden':
          $field_title .= ' (' . t('hidden') . ')';
          $field_description = t('This group will not be visible to the end user. However you can set the default value and it will be actually used.');
          if ($mode == 'admin') {
            $field_type = 'checkboxes';
          }
          else {
            $field_type = 'value';
            $value = isset($defaults[$group->id]) ? $defaults[$group->id] : [];
          }
          break;
        case 'radio':
          $field_type = 'radios';
          break;
        case 'dropdown':
          $field_type = 'select';
          break;
        default:
          $field_type = $group->type;
      }

      // Extract the field options:
      $options = [];
      if ($field_type == 'select') {
        $options[''] = '-- select --';
      }
      $default_values = [];

      // Set interest options and default values.
      foreach ($interest_data->interests as $interest) {

        // phpcs:disable
        $options[$interest->id] = t($interest->name);

        // phpcs:enable
        if (isset($memberinfo)) {
          if (isset($memberinfo->interests->{$interest->id}) && $memberinfo->interests->{$interest->id} === TRUE) {
            $default_values[$group->id][] = $interest->id;
          }
        }
        elseif (!empty($defaults)) {
          if ($group->type === 'radio') {
            if (isset($defaults[$group->id]) && $defaults[$group->id] === $interest->id) {
              $default_values[$group->id] = $interest->id;
            }
          }
          else {
            if (isset($defaults[$group->id][$interest->id]) && !empty($defaults[$group->id][$interest->id])) {
              $default_values[$group->id][] = $interest->id;
            }
          }
        }
      }
      $return[$group->id] = [
        '#type' => $field_type,
        '#title' => $field_title,
        '#description' => $field_description,
        '#options' => $options,
        '#default_value' => isset($default_values[$group->id]) ? $default_values[$group->id] : [],
        '#attributes' => [
          'class' => [
            'mailchimp-newsletter-interests-' . $list->id,
          ],
        ],
      ];
      if ($value !== NULL) {
        $return[$group->id]['#value'] = $value;
      }
    }
  } catch (\Exception $e) {
    \Drupal::logger('mailchimp')
      ->error('An error occurred generating interest group lists. "{message}"', [
      'message' => $e
        ->getMessage(),
    ]);
  }
  return $return;
}