public function MailchimpListsSubscribeForm::buildForm in Mailchimp 8
Same name and namespace in other branches
- 2.x modules/mailchimp_lists/src/Form/MailchimpListsSubscribeForm.php \Drupal\mailchimp_lists\Form\MailchimpListsSubscribeForm::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_lists/ src/ Form/ MailchimpListsSubscribeForm.php, line 88
Class
- MailchimpListsSubscribeForm
- Subscribe to a Mailchimp list/audience.
Namespace
Drupal\mailchimp_lists\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$field_settings = $this->fieldInstance
->getFieldDefinition()
->getSettings();
$field_formatter_settings = $this->fieldFormatter
->getSettings();
$mc_list = mailchimp_get_list($field_settings['mc_list_id']);
$email = mailchimp_lists_load_email($this->fieldInstance, $this->fieldInstance
->getEntity());
if (!$email) {
return [];
}
$field_name = $this->fieldInstance
->getFieldDefinition()
->getName();
// Determine if a user is subscribed to the list.
$is_subscribed = mailchimp_is_subscribed($mc_list['id'], $email);
$wrapper_key = 'mailchimp_' . $field_name;
$form['wrapper_key'] = [
'#type' => 'hidden',
'#default_value' => $wrapper_key,
];
$form[$wrapper_key] = [
'#type' => 'container',
'#tree' => TRUE,
'#description' => $this->fieldInstance
->getFieldDefinition()
->getDescription(),
'#attributes' => [
'class' => [
'mailchimp-newsletter-wrapper',
'mailchimp-newsletter-' . $field_name,
],
],
];
// Add title and description to lists for anonymous users or if requested:
$form[$wrapper_key]['subscribe'] = [
'#type' => 'checkbox',
'#title' => 'Subscribe',
'#disabled' => $this->fieldInstance
->getFieldDefinition()
->isRequired(),
'#required' => $this->fieldInstance
->getFieldDefinition()
->isRequired(),
'#default_value' => $this->fieldInstance
->getFieldDefinition()
->isRequired() || $is_subscribed,
];
// Present interest groups:
if ($field_settings['show_interest_groups'] && $field_formatter_settings['show_interest_groups']) {
// Perform test in case error comes back from MCAPI when getting groups:
if (is_array($mc_list['intgroups'])) {
$form[$wrapper_key]['interest_groups'] = [
'#type' => 'fieldset',
'#title' => isset($field_settings['interest_groups_label']) ? $field_settings['interest_groups_label'] : $this
->t('Interest Groups'),
'#weight' => 100,
'#states' => [
'invisible' => [
':input[name="' . $wrapper_key . '[subscribe]"]' => [
'checked' => FALSE,
],
],
],
];
$groups_default = $this->fieldInstance
->getInterestGroups();
if ($groups_default == NULL) {
$groups_default = [];
}
$form[$wrapper_key]['interest_groups'] += mailchimp_interest_groups_form_elements($mc_list, $groups_default, $email);
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
];
return $form;
}