0

I have added a submission handler in a .module file to execute some custom code when a webform is submitted.

function mh_newsletter_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'webform_submission_newsletter_node_26_add_form') {
    $form['#validate'][] = 'mh_newsletter_form_validate';
    $form['actions']['submit']['#submit'][] = 'mh_newsletter_form_submit';
  }
}

function mh_newsletter_form_submit($form, &$form_state) {
  // Custom code
}

I'm unable to access the webform submission ID. How can I get that value?

0
2

To get the webform submission ID in a custom form handler, I used the following code.

function MODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id== 'webform_submission_add_form') {
    $form['actions']['submit']['#submit'][] = 'MODULE_custom_submit_handler';
  }
}

function MODULE_custom_submit_handler(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $webform_submission_id = $form_state->getFormObject()->getEntity()->id();
 }
1

Add a webform handler to the webform configuration and use the method submitForm() of the handler, which provides the submission entity as argument:

mymodule/src/Plugin/WebformHandler/MyWebformHandler.php:

<?php

namespace Drupal\mymodule\Plugin\WebformHandler;

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

/**
 * My webform submission handler.
 *
 * @WebformHandler(
 *   id = "my_webform_handler",
 *   label = @Translation("My Webform Handler"),
 *   category = @Translation("Example"),
 *   description = @Translation("Example of a webform handler."),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_SINGLE,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL,
 * )
 */
class MyWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    $submission_id = $webform_submission->id();
    // my submit logic
  }

}

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.