public function WebformMailChimpHandler::postSave in Webform Mailchimp 8.5
Acts on a saved webform submission before the insert or update hook is invoked.
Parameters
\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.
bool $update: TRUE if the entity has been updated, or FALSE if it has been inserted.
Overrides WebformHandlerBase::postSave
File
- src/
Plugin/ WebformHandler/ WebformMailChimpHandler.php, line 263
Class
- WebformMailChimpHandler
- Form submission to MailChimp handler.
Namespace
Drupal\webform_mailchimp\Plugin\WebformHandlerCode
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
// If update, do nothing
if ($update) {
return;
}
$fields = $webform_submission
->toArray(TRUE);
// If there's a checkbox configured, check for its value
if (!empty($this->configuration['control']) && empty($fields['data'][$this->configuration['control']])) {
return;
}
$configuration = $this->tokenManager
->replace($this->configuration, $webform_submission);
// Email could be a webform element or a string/token.
if (!empty($fields['data'][$configuration['email']])) {
$email = $fields['data'][$configuration['email']];
}
else {
$email = $configuration['email'];
}
$mergevars = Yaml::decode($configuration['mergevars']) ?? [];
// Allow other modules to alter the merge vars.
// @see hook_mailchimp_lists_mergevars_alter().
$entity_type = 'webform_submission';
\Drupal::moduleHandler()
->alter('mailchimp_lists_mergevars', $mergevars, $webform_submission, $entity_type);
\Drupal::moduleHandler()
->alter('webform_mailchimp_lists_mergevars', $mergevars, $webform_submission, $this);
$handler_link = Link::createFromRoute(t('Edit handler'), 'entity.webform.handler.edit_form', [
'webform' => $this
->getWebform()
->id(),
'webform_handler' => $this
->getHandlerId(),
])
->toString();
$submission_link = $webform_submission
->toLink($this
->t('Edit'), 'edit-form')
->toString();
$context = [
'link' => $submission_link . ' / ' . $handler_link,
'webform_submission' => $webform_submission,
'handler_id' => $this
->getHandlerId(),
];
if (!empty($configuration['list']) && !empty($email)) {
$member_data = mailchimp_get_memberinfo($configuration['list'], $email, TRUE);
// If the user is already subscribed, do not set it back to pending
$double_optin = $configuration['double_optin'];
if (!empty($member_data->status) && $member_data->status == MailchimpLists::MEMBER_STATUS_SUBSCRIBED) {
$double_optin = FALSE;
}
mailchimp_subscribe($configuration['list'], $email, array_filter($mergevars, 'strlen'), $configuration['interest_groups'], $double_optin);
}
else {
if (empty($configuration['list'])) {
\Drupal::logger('webform_submission')
->warning('No mailchimp list was provided to the handler.', $context);
}
if (empty($email)) {
\Drupal::logger('webform_submission')
->warning('No email address was provided to the handler.', $context);
}
}
}