protected function CampaignMonitorSubscribeForm::singleSubscribeForm in Campaign Monitor 8
Same name and namespace in other branches
- 8.2 src/Form/CampaignMonitorSubscribeForm.php \Drupal\campaignmonitor\Form\CampaignMonitorSubscribeForm::singleSubscribeForm()
1 call to CampaignMonitorSubscribeForm::singleSubscribeForm()
- CampaignMonitorSubscribeForm::buildForm in src/
Form/ CampaignMonitorSubscribeForm.php - Form constructor.
File
- src/
Form/ CampaignMonitorSubscribeForm.php, line 104
Class
- CampaignMonitorSubscribeForm
- Subscribe to a campaignmonitor list.
Namespace
Drupal\campaignmonitor\FormCode
protected function singleSubscribeForm(array $form, FormStateInterface $form_state, $config = []) {
$list_id = $config['list_id'];
$current_user = \Drupal::currentUser();
$list = campaignmonitor_get_extended_list_settings($list_id);
$list_options = campaignmonitor_get_list_settings($list_id);
// Set options for the form.
$form = [
'#tree' => TRUE,
'#attributes' => [
'class' => [
'campaignmonitor-subscribe-form',
'campaignmonitor-subscribe-form-' . str_replace(' ', '-', strtolower($list['name'])),
],
],
];
if ($config['list_id_text'] != '') {
$text = str_replace('@name', $list['name'], $config['list_id_text']);
$form['selection'] = [
'#markup' => $text,
];
}
// Should the name field be displayed for this user.
if (isset($list_options['display']['name']) && $list_options['display']['name']) {
// Token replace if the token module is present.
if (isset($list_options['tokens']['name']) && \Drupal::moduleHandler()
->moduleExists('token') && $current_user
->id() > 0) {
$name = \Drupal::token()
->replace($list_options['tokens']['name'], [
'user' => User::load($current_user
->id()),
], [
'clear' => TRUE,
]);
}
// Check if the user is subscribed and get name from Campaign Monitor.
if (!empty($email) && campaignmonitor_is_subscribed($list_id, $email)) {
// If subscribed, get her/his name from Campaign Monitor.
$subscriber = campaignmonitor_get_subscriber($list_id, $email);
$name = isset($subscriber['Name']) ? $subscriber['Name'] : $name;
}
$form['name'] = [
'#type' => 'email',
'#title' => t('Name'),
'#required' => TRUE,
'#maxlength' => 200,
'#default_value' => isset($name) ? $name : '',
];
}
if (isset($list['CustomFields'])) {
foreach ($list['CustomFields'] as $key => $field) {
// Form API can't handle keys with [] in all cases.
$form_key = str_replace([
'[',
']',
], '', $key);
// Check if field should be displayed.
if (isset($list_options['CustomFields']) && !$list_options['CustomFields']['selected'][$form_key]) {
// Field is not selected, so continue.
continue;
}
// Token replace default value, if the token module is present.
$token = '';
if (\Drupal::moduleHandler()
->moduleExists('token') && isset($list_options['tokens'][$form_key])) {
$token_user = $current_user
->id() > 0 ? User::load($current_user
->id()) : NULL;
$token = \Drupal::token()
->replace($list_options['tokens'][$form_key], [
'user' => $token_user,
], [
'clear' => TRUE,
]);
}
switch ($field['DataType']) {
case 'Text':
$form['CustomFields'][$form_key] = [
'#type' => 'textfield',
'#title' => SafeMarkup::checkPlain($field['FieldName']),
'#maxlength' => 200,
'#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
];
break;
case 'MultiSelectOne':
$options = [];
foreach ($field['FieldOptions'] as $option) {
$options[$option] = $option;
}
$form['CustomFields'][$form_key] = [
'#type' => 'select',
'#title' => SafeMarkup::checkPlain($field['FieldName']),
'#options' => $options,
'#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
];
break;
case 'MultiSelectMany':
$options = [];
foreach ($field['FieldOptions'] as $option) {
$options[$option] = $option;
}
// If one value was selected, default is a string else an array.
$cm_default = isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : [];
// Exspensive.
$is_array = is_array($cm_default);
$default = [];
foreach ($options as $value) {
if ($is_array) {
if (in_array($value, $cm_default)) {
$default[$value] = $value;
}
}
elseif ($cm_default == $value) {
$default[$cm_default] = $cm_default;
}
else {
$default[$value] = 0;
}
}
$form['CustomFields'][$form_key] = [
'#type' => 'checkboxes',
'#title' => SafeMarkup::checkPlain($field['FieldName']),
'#options' => $options,
'#default_value' => $default,
];
break;
case 'Number':
$form['CustomFields'][$form_key] = [
'#type' => 'textfield',
'#title' => SafeMarkup::checkPlain($field['FieldName']),
'#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
];
break;
case 'Date':
// Load jQuery datepicker to ensure the right date format.
drupal_add_library('system', 'ui.datepicker');
$form['CustomFields'][$form_key] = [
'#type' => 'date_popup',
'#title' => SafeMarkup::checkPlain($field['FieldName']),
'#default_value' => isset($subscriber['CustomFields'][$field['FieldName']]) ? $subscriber['CustomFields'][$field['FieldName']] : $token,
'#attributes' => [
'class' => [
'campaignmonitor-date',
],
],
];
break;
}
}
}
$form['list_id'] = [
'#type' => 'hidden',
'#default_value' => $list_id,
];
return $form;
}