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.


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 ?

  • This information is already available via Tokens in your Email/Handlers. [current-page:url:args] The specific argument of the current page (e.g. 'arg:1' on the page 'node/1' returns '1'). – stacey.mosier Apr 8 '19 at 17:59
  • I am aware of the tokens but I need to do the explained above via a custom code... Can you clarify more on how to use the tokens from inside the custom code ? Thank you – Elie Masaad Apr 8 '19 at 18:40

I have found an answer here: https://www.drupal.org/project/webform/issues/3046808

if ($source_entity = $webform_submission->getSourceEntity()) {
   $nid = $source_entity->id();
else {
  $nid = NULL;

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.