You are here

public function MailchimpSignupPageForm::buildForm in Mailchimp 2.x

Same name and namespace in other branches
  1. 8 modules/mailchimp_signup/src/Form/MailchimpSignupPageForm.php \Drupal\mailchimp_signup\Form\MailchimpSignupPageForm::buildForm()

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_signup/src/Form/MailchimpSignupPageForm.php, line 91

Class

MailchimpSignupPageForm
Subscribe to a Mailchimp list/audience.

Namespace

Drupal\mailchimp_signup\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = [];
  $settings = $this->signup->settings;
  $form['#attributes'] = [
    'class' => [
      'mailchimp-signup-subscribe-form',
    ],
  ];
  $form['description'] = [
    '#markup' => $this->signup->description,
  ];

  // If the form is configured to submit via AJAX, add an empty container
  // element. This element is going to be replaced with the returned AJAX
  // response.
  if (isset($this->signup->settings['ajax_submit']) && $this->signup->settings['ajax_submit']) {
    $form['response'] = [
      '#prefix' => '<div id="mailchimp-response-' . $this->formId . '-wrapper">',
      '#suffix' => '</div>',
    ];
  }
  $form['mailchimp_lists'] = [
    '#tree' => TRUE,
  ];
  $lists = mailchimp_get_lists($this->signup->mc_lists);
  $lists_count = !empty($lists) ? count($lists) : 0;
  if (empty($lists)) {
    return [
      'message' => [
        '#markup' => $this
          ->t('The subscription service is currently unavailable. Please check again later.'),
      ],
    ];
  }
  $list = [];
  if ($lists_count > 1) {

    // Default behavior.
    // The only difference here is that we've moved the default
    // value here, as a variable.
    $show_lists_and_groups = TRUE;

    // If we have selected the new option to pre-configure the
    // interest groups, that means that we need to hide the
    // container alongside the rendered checkboxes from the user.
    if (isset($this->signup->settings['configure_groups']) && $this->signup->settings['configure_groups']) {
      $show_lists_and_groups = FALSE;
    }
    foreach ($lists as $list) {

      // Wrap in a div:
      $wrapper_key = 'mailchimp_' . $list->id;
      $subscribe_to_list = FALSE;

      // If we have selected the pre-configure option, we need to populate
      // the data the same way, before introducing the new functionality.
      if (isset($settings['configure_groups']) && isset($settings['group_items'])) {
        if (array_key_exists($list->id, $settings['group_items'])) {
          $subscribe_to_list = $list->id;
        }
      }
      $form['mailchimp_lists'][$wrapper_key] = [
        '#prefix' => '<div id="mailchimp-newsletter-' . $list->id . '" class="mailchimp-newsletter-wrapper">',
        '#suffix' => '</div>',
      ];
      $form['mailchimp_lists'][$wrapper_key]['subscribe'] = [
        '#type' => 'checkbox',
        '#title' => $list->name,
        '#return_value' => $list->id,
        '#default_value' => $subscribe_to_list,
        '#access' => $show_lists_and_groups,
      ];
      if ($this->signup->settings['include_interest_groups'] && isset($list->intgroups)) {
        $form['mailchimp_lists'][$wrapper_key]['interest_groups'] = [
          '#type' => 'fieldset',
          '#access' => $show_lists_and_groups,
          '#title' => $this
            ->t('Interest Groups for %label', [
            '%label' => $list->name,
          ]),
          '#states' => [
            'invisible' => [
              ':input[name="mailchimp_lists[' . $wrapper_key . '][subscribe]"]' => [
                'checked' => FALSE,
              ],
            ],
          ],
        ];

        // Create the form elements for all interest groups
        // and select the ones needed.
        $defaults = [];
        $groups_items = isset($this->signup->settings['group_items']) ? $this->signup->settings['group_items'] : [];
        if (isset($groups_items[$list->id])) {
          $defaults = $groups_items[$list->id];
        }
        $form['mailchimp_lists'][$wrapper_key]['interest_groups'] += mailchimp_interest_groups_form_elements($list, $defaults);
      }
    }
  }
  else {
    $list = reset($lists);

    // Default behavior.
    // The only difference here is that we've moved the default
    // value here, as a variable.
    $show_lists_and_groups = TRUE;

    // If we have selected the new option to pre-configure the
    // interest groups, that means that we need to hide the
    // container alongside the rendered checkboxes from the user.
    if (isset($this->signup->settings['configure_groups']) && $this->signup->settings['configure_groups']) {
      $show_lists_and_groups = FALSE;
    }

    // Create the form elements for all interest groups
    // and select the ones needed.
    $defaults = [];
    $groups_items = isset($this->signup->settings['group_items']) ? $this->signup->settings['group_items'] : [];
    if (isset($groups_items[$list->id])) {
      $defaults = $groups_items[$list->id];
    }
    if ($this->signup->settings['include_interest_groups'] && isset($list->intgroups)) {
      $form['mailchimp_lists']['#weight'] = 9;
      $form['mailchimp_lists']['interest_groups'] = mailchimp_interest_groups_form_elements($list, $defaults);
      $form['mailchimp_lists']['#access'] = $show_lists_and_groups;
    }
  }
  $mergevars_wrapper_id = isset($list->id) ? $list->id : '';
  $form['mergevars'] = [
    '#prefix' => '<div id="mailchimp-newsletter-' . $mergevars_wrapper_id . '-mergefields" class="mailchimp-newsletter-mergefields">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  ];
  foreach ($this->signup->settings['mergefields'] as $tag => $mergevar_str) {
    if (!empty($mergevar_str)) {
      $mergevar = unserialize($mergevar_str);
      $form['mergevars'][$tag] = mailchimp_insert_drupal_form_tag($mergevar);
      if (empty($lists)) {
        $form['mergevars'][$tag]['#disabled'] = TRUE;
      }
    }
  }

  // Include the GDPR consent checkbox if necessary
  if (!empty($this->signup->settings['gdpr_consent'])) {
    $form['gdpr_consent'] = [
      '#type' => 'checkbox',
      '#default_value' => FALSE,
      '#title' => $this->signup->settings['gdpr_checkbox_label'],
      '#required' => isset($this->signup->settings['gdpr_consent_required']) ? $this->signup->settings['gdpr_consent_required'] : FALSE,
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t($this->signup->settings['submit_button']),
    '#disabled' => empty($lists),
  ];

  // Add a wrapper element to the form if it is configured for AJAX submits.
  if (isset($this->signup->settings['ajax_submit']) && $this->signup->settings['ajax_submit']) {
    $form_wrapper = Html::getId($this->formId);
    $response_wrapper = "mailchimp-response-{$this->formId}-wrapper";
    $form['actions']['submit']['#id'] = $form_wrapper . '-edit-submit';
    $form['#prefix'] = '<div id="' . $form_wrapper . '">';
    $form['#suffix'] = '</div>';
    $form['actions']['submit']['#ajax'] = [
      'callback' => '::ajaxSubmit',
      'response_wrapper' => $response_wrapper,
    ];
  }
  return $form;
}