Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commented@see \Drupal\webform\WebformSubmissionInterface::getSourceEntity
Comment #3
KarthickAcsh_R CreditAttribution: KarthickAcsh_R commentedHi @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
Thank you
Comment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at The Big Blue House commentedYou need to use
$form_state->getFormObject();
. The below code should point you in the right direction.