public function AddMailchimpEventWebformHandler::buildConfigurationForm in Mailchimp 2.x
Form constructor.
Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.
Parameters
array $form: An associative array containing the initial structure of the plugin form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Return value
array The form structure.
Overrides WebformHandlerBase::buildConfigurationForm
File
- modules/
mailchimp_events/ src/ Plugin/ WebformHandler/ AddMailchimpEventWebformHandler.php, line 79
Class
- AddMailchimpEventWebformHandler
- Webform submission Event handler.
Namespace
Drupal\mailchimp_events\Plugin\WebformHandlerCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$mc_lists = mailchimp_get_lists();
$list_options = [];
foreach ($mc_lists as $key => $value) {
$list_options[$key] = $value->name;
}
$events = MailchimpEvent::loadMultiple();
foreach ($events as $key => $event) {
$event_options[$event
->id()] = $event
->getName();
}
$form['events'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Events'),
];
$form['events']['list'] = [
'#type' => 'select',
'#title' => $this
->t('Mailchimp audience'),
'#weight' => '0',
'#required' => TRUE,
'#options' => $list_options,
'#default_value' => $this->configuration['list'] ?: FALSE,
];
// Options:
// Logged-in user email address
// An email field on the form
// A token
// A static value.
$form['events']['email_type'] = [
'#type' => 'radios',
'#title' => $this
->t('Email'),
'#description' => $this
->t('Which email address to send'),
'#options' => [
'user' => $this
->t('The logged in user'),
'form_email' => $this
->t('An email on the form'),
'single' => $this
->t('A specific email address'),
'token' => $this
->t('A token'),
],
'#weight' => '0',
'#required' => TRUE,
'#default_value' => $this->configuration['email_type'] ?: 'user',
];
$form_emails = [];
$elements = $this->webform
->getElementsInitializedAndFlattened();
foreach ($elements as $element_key => $element) {
if ($element['#type'] == 'email') {
$form_emails['[webform_submission:values:' . $element_key . ']'] = $element_key . ':"' . $element['#title'] . '"';
}
}
$form['events']['form_email'] = [
'#type' => 'select',
'#title' => $this
->t('Email'),
'#description' => $this
->t('An email address element on the webform.'),
'#options' => $form_emails,
'#weight' => '0',
'#states' => [
'visible' => [
':input[name="settings[email_type]"]' => [
'value' => 'form_email',
],
],
],
'#default_value' => $this->configuration['email'] && $this->configuration['email_type'] == 'form_email' ? $this->configuration['email'] : '',
];
$form['events']['single'] = [
'#type' => 'email',
'#title' => $this
->t('Email'),
'#description' => $this
->t('The email address to associate with this event.'),
'#weight' => '0',
'#states' => [
'visible' => [
':input[name="settings[email_type]"]' => [
'value' => 'single',
],
],
],
'#default_value' => $this->configuration['email'] && $this->configuration['email_type'] == 'single' ? $this->configuration['email'] : '',
];
$form['events']['token'] = [
'#type' => 'textfield',
'#title' => $this
->t('Token'),
'#description' => $this
->t('A token containing the email address to associate with this event.'),
'#weight' => '0',
'#states' => [
'visible' => [
':input[name="settings[email_type]"]' => [
'value' => 'token',
],
],
],
'#element_validate' => [
'token_element_validate',
],
'#default_value' => $this->configuration['email'] && $this->configuration['email_type'] == 'token' ? $this->configuration['email'] : '',
];
// If the event has been deleted, the event name is no longer valid.
$add_link = Link::createFromRoute('You can add Events here.', 'entity.mailchimp_event.add_form', [], [
'attributes' => [
'target' => '_blank',
],
]);
$form['events']['event_name'] = [
'#type' => 'select',
'#title' => $this
->t('Event Name'),
'#description' => $this
->t('The name of the Event. %add_link', [
'%add_link' => $add_link
->toString(),
]),
'#weight' => '0',
'#required' => TRUE,
'#options' => $event_options,
'#default_value' => $this->configuration['event_name'] ?: FALSE,
'#ajax' => [
'callback' => [
$this,
'getMailchimpEventProperty',
],
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper' => 'property-holder',
'progress' => [
'type' => 'throbber',
'message' => $this
->t('Verifying entry...'),
],
],
];
$form['events']['event_value'] = [
'#type' => 'details',
'#title' => $this
->t('Properties'),
'#description' => $this
->t('Properties can be any string. This field supports tokens.'),
'#open' => TRUE,
'#prefix' => '<div id="property-holder">',
'#suffix' => '</div>',
];
if (isset($form_state
->getValues()['event_name'])) {
$event_id = $form_state
->getValues()['event_name'];
}
elseif ($this->configuration['event_name'] && $this
->isValidEventId($this->configuration['event_name'])) {
$event_id = $this->configuration['event_name'];
}
else {
$event_id = array_key_first($event_options);
}
$form['events']['event_value']['properties'] = $this
->getEventPropertiesById($event_id);
$form['events']['is_syncing'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Suppress automations in Mailchimp?'),
'#description' => $this
->t('Events will be logged, but automations will not trigger.'),
'#default_value' => isset($this->configuration['is_syncing']) ? $this->configuration['is_syncing'] : FALSE,
];
// Development.
$form['development'] = [
'#type' => 'details',
'#title' => $this
->t('Development settings'),
];
$form['development']['debug'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable debugging'),
'#description' => $this
->t('If checked, trigger events will be displayed onscreen to all users.'),
'#return_value' => TRUE,
'#default_value' => $this->configuration['debug'],
];
$this
->elementTokenValidate($form);
return $this
->setSettingsParents($form);
}