2

Is there a way to trigger the Emails/Handlers, When webform submission has been updated. I'm using webform_mailchimp as a handler. When I'm submitting a new record, handler is working fine and I can receive the data into my Mailchimp account. I have a function to update the submission via code. But it doesn't trigger, when I update the same record. Following is my code to update the webform submission:

$webform = Webform::load('my_form');
$is_open = WebformSubmissionForm::isOpen($webform);
$sid = 2;

if ($is_open === TRUE) {
    // Load submission
    $webform_submission = WebformSubmission::load($sid);

    // Modify submission values
    $webform_submission->setElementData('preferred_date', $form_state->getValue('preferredDate'));
    $webform_submission->setElementData('preferred_time', $form_state->getValue('preferredTime'));

    // Validate submission.
    $errors = WebformSubmissionForm::validateWebformSubmission($webform_submission);

    // Check there are no validation errors.
    if (!empty($errors)) {
        drupal_set_message(t('Failed! Please contact the administrator.'), 'error');
    }
    else {
        // Submit values and get submission ID.
        $webform_submission = WebformSubmissionForm::submitWebformSubmission($webform_submission);
        drupal_set_message(t("Success! ".$webform_submission->id()), 'status');
    }
}
  1. Is there a way to trigger the handler through code(programmatically)?
  2. Is there a default configuration in the CMS?

Thanks guys,

2

The below code is untested but should point you in the right direction.

// Get a handler.
$webform_submission = WebformSubmission::load(WEBFORM_ID)
$handlers = $webform_submission->getWebform()->getHandlers();
$handler = $handlers[HANDLER_ID];

// Invoke submit form.
@see \Drupal\webform\Plugin\WebformHandlerInterface::submitForm
$form = []; // Might need to get the $form.
$form_state = new FormState(); // Might need to be build the form state.
$handler->submitForm($form, $form_state, $webform_submission);

@see \Drupal\webform\Entity\Webform::invokeHandlers

@see \Drupal\webform\Entity\WebformSubmission::invokeWebformHandlers

  • How do I get $form and $form_state from $webform or $webform_submission objects? Or do I need to build them in another way? My code is in a class which extends the ControllerBase class... – cchen Aug 15 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.