I have a Webform attached as a field to a specific content type.

After the node is loaded and the Webform is submitted, I am using the below custom webform handler to get the $webform_submission values.

<?php

namespace Drupal\my_custom_module\Plugin\WebformHandler;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Form\FormStateInterface;

use Drupal\node\Entity\Node;

use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;


/**
 * Do something when a webform submission is submitted.
 *
 * @WebformHandler(
 *   id = "Do something",
 *   label = @Translation("Do something"),
 *   category = @Translation("any category"),
 *   description = @Translation("Do something"),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
 * )
 */

class customWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */

  // Function to be fired while submitting the Webform.
  public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
    // Get an array of the values from the submission.
    $values = $webform_submission->getData();

    // Get (Full Name) from (full_name) text element available on the webform.
    $fullnamevalue = $values["full_name"];

However, I would like to get the "nid" of that node from where the Webform is submitted, so I can load this node by nid to use it in my custom code.

How to get the "nid" of that node from where the Webform is submitted ?

Comments

Drutech created an issue. See original summary.

jrockowitz’s picture

Status: Active » Closed (works as designed)

@see \Drupal\webform\WebformSubmissionInterface::getSourceEntity

if ($source_entity = $webform_submission->getSourceEntity()) {
   $nid = $source_entity->id();
}
else {
  $nid = NULL;
}
KarthickAcsh_R’s picture

Hi @jrockowitz
In my case, I wrote a custom form submit handler for a webform in a custom module hook, Now Need to get the webform submission id there, this same approach is not working for me, so pls help me out, Here is my code

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\webform\WebformInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\webformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;


function pay_form_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  if ($form_id == 'webform_submission_product_add_form') {

  $form['actions']['submit']['#submit'][] = 'custom_submit_handler';
}

}

function custom_submit_handler(&$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {

	$source = $webform_submission->getSourceEntity();
	$nid = $source->id();
	kint($nid);

}

Thank you

jrockowitz’s picture

You need to use $form_state->getFormObject();. The below code should point you in the right direction.


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

function custom_submit_handler(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  /** @var \Drupal\webform\WebformSubmissionForm $form_object */
  $form_object = $form_state->getFormObject();
  /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
  $webform_submission = $form_object->getEntity();
  $source = $webform_submission->getSourceEntity();
  $nid = $source->id();
  kint($nid);
}