public function MailchimpListsSubscription::fieldSettingsForm in Mailchimp 2.x
Same name and namespace in other branches
- 8 modules/mailchimp_lists/src/Plugin/Field/FieldType/MailchimpListsSubscription.php \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription::fieldSettingsForm()
Returns a form for the field-level settings.
Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.
Parameters
array $form: The form where the settings form is being included in.
\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.
Return value
array The form definition for the field settings.
Overrides FieldItemBase::fieldSettingsForm
File
- modules/
mailchimp_lists/ src/ Plugin/ Field/ FieldType/ MailchimpListsSubscription.php, line 161
Class
- MailchimpListsSubscription
- Plugin implementation of the 'mailchimp_lists_subscription' field type.
Namespace
Drupal\mailchimp_lists\Plugin\Field\FieldTypeCode
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$element = parent::fieldSettingsForm($form, $form_state);
$mc_list_id = $this
->getFieldDefinition()
->getSetting('mc_list_id');
if (empty($mc_list_id)) {
\Drupal::messenger()
->addError($this
->t('Select an audience to sync with on the Field Settings tab before configuring the field instance.'));
return $element;
}
$this->definition;
$instance_settings = $this->definition
->getSettings();
$element['subscribe_checkbox_label'] = [
'#title' => 'Subscribe Checkbox Label',
'#type' => 'textfield',
'#default_value' => isset($instance_settings['subscribe_checkbox_label']) ? $instance_settings['subscribe_checkbox_label'] : 'Subscribe',
];
$element['show_interest_groups'] = [
'#title' => "Enable Interest Groups",
'#type' => "checkbox",
'#default_value' => $instance_settings['show_interest_groups'],
];
$element['hide_subscribe_checkbox'] = [
'#title' => $this
->t('Hide Subscribe Checkbox for new subscribers'),
'#type' => 'checkbox',
'#default_value' => $instance_settings['hide_subscribe_checkbox'],
'#description' => $this
->t('When Interest Groups are enabled, the "subscribe" checkbox is hidden and selecting any interest group will subscribe a user to the audience.'),
'#states' => [
'visible' => [
'input[name="settings[show_interest_groups]"]' => [
'checked' => TRUE,
],
],
],
];
$element['interest_groups_hidden'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Hide Interest Groups.'),
'#description' => $this
->t('If checked, the Interest Groups will not be displayed, but the default values will be used.'),
'#default_value' => $instance_settings['interest_groups_hidden'],
'#states' => [
'visible' => [
'input[name="settings[show_interest_groups]"]' => [
'checked' => TRUE,
],
],
],
];
$element['interest_groups_label'] = [
'#title' => "Interest Groups Label",
'#type' => "textfield",
'#default_value' => !empty($instance_settings['interest_groups_label']) ? $instance_settings['interest_groups_label'] : 'Interest Groups',
];
$element['merge_fields'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Merge Fields'),
'#description' => $this
->t('Multi-value fields will only sync their first value to Mailchimp, as Mailchimp does not support multi-value fields.'),
'#tree' => TRUE,
];
$element['unsubscribe_on_delete'] = [
'#title' => "Unsubscribe on deletion",
'#type' => "checkbox",
'#description' => $this
->t('Unsubscribe entities from this audience when they are deleted.'),
'#default_value' => $instance_settings['unsubscribe_on_delete'],
];
$mv_defaults = $instance_settings['merge_fields'];
$mergevars = mailchimp_get_mergevars([
$mc_list_id,
]);
$field_config = $this
->getFieldDefinition();
$fields = $this
->getFieldmapOptions($field_config
->get('entity_type'), $field_config
->get('bundle'));
$required_fields = $this
->getFieldmapOptions($field_config
->get('entity_type'), $field_config
->get('bundle'), TRUE);
// Prevent this subscription field appearing as a merge field option.
$field_name = $this
->getFieldDefinition()
->getName();
unset($fields[$field_name]);
$fields_flat = OptGroup::flattenOptions($fields);
foreach ($mergevars[$mc_list_id] as $mergevar) {
$default_value = isset($mv_defaults[$mergevar->tag]) ? $mv_defaults[$mergevar->tag] : -1;
$element['merge_fields'][$mergevar->tag] = [
'#type' => 'select',
'#title' => Html::escape($mergevar->name),
'#default_value' => array_key_exists($default_value, $fields_flat) ? $default_value : '',
'#required' => $mergevar->required,
];
if (!$mergevar->required || $mergevar->tag === 'EMAIL') {
$element['merge_fields'][$mergevar->tag]['#options'] = $fields;
if ($mergevar->tag === 'EMAIL') {
$element['merge_fields'][$mergevar->tag]['#description'] = $this
->t('Any entity with an empty or invalid email address field value will simply be ignored by the Mailchimp subscription system. <em>This is why the Email field is the only required merge field which can sync to non-required fields.</em>');
}
}
else {
$element['merge_fields'][$mergevar->tag]['#options'] = $required_fields;
$element['merge_fields'][$mergevar->tag]['#description'] = $this
->t("Only 'required' and 'calculated' fields are allowed to be synced with Mailchimp 'required' merge fields.");
}
}
return $element;
}