0

I have added a submit handler function in my .module file so i can execute some custom code when a form(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) {
  //My logic
}

but i am not able to access the webform submission id. Any help?

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.