3

I have created a couple of webform submit handlers before, but my new one fires on every page submit rather than when the last page is submitted.

How do I get the webform handler to only fire after the full form is submitted?

Below is what I have at the moment, which fires on each page:

namespace Drupal\the_api\Plugin\WebformHandler;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Webform submission remote post handler.
 *
 * @WebformHandler(
 *   id = "membership_api_post",
 *   label = @Translation("New Membership to API"),
 *   category = @Translation("External"),
 *   description = @Translation("Posts a new membership through to the API."),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */
class NewMembershipWebformHandler extends WebformHandlerBase {

  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   */
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    parent::submitForm($form, $form_state, $webform_submission);

    kint($form_state);

  }
}
4

You can use $webform_submission->getState() to determine if the submission is completed. The EmailWebformHandler includes decent examples on how to the submission state. I recommend trying to use WebformHandler::postSave() instead of WebformHandler::formSubmit().

| improve this answer | |
  • Thanks I was trying to tweak the @WebformHandler settings, switching to the postSave() method did the trick. – Duncanmoo Dec 1 '17 at 8:35
  • I ran into an issue where postSave was firing the handlers twice. One of the handlers (webform_pardot) creates it's own submission entity and attaches the webform submission entity via entity reference. When the pardot entity was saved, also saving the webform submission, it fired all of the webform handlers that implemented postSave. Better to go with submitForm and getState(). – Scott Joudry Feb 5 at 1:14
  • To prevent retriggering all handlers when trying to resave submission via a handler, use \Drupal\webform\Entity\WebformSubmission::resave – jrockowitz Feb 6 at 13:08
1

As I noted in the comments of the answer by jrockowitz, postSave() will fire multiple times in certain situations. I ran into this issue after installing the webform_pardot module, which creates it's own submission entity that references the original webform submission entity via an entity reference field. When the pardot submission is saved, the webform entity is saved as well and postSave fires. Shortly after that the webform submission is completed/saved and postSave fires again.

The following should work for all situations:

  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

    // This check is done to ensure that multi-page webform submissions do not
    // fire this code when the entire form is submitted. If the current page is
    // set to 'completed' then the multi-page form has been completely submitted.
    // When a single page webform is submitted the current page will return an
    // empty string and that condition will skip this test.
    $current_page = $webform_submission->getCurrentPage();
    if (!empty($current_page) && $current_page != WebformSubmissionInterface::STATE_COMPLETED) {
      return;
    }
  }
| improve this answer | |

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.