0

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 ?

2
  • 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'). Apr 8, 2019 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 Apr 8, 2019 at 18:40

2 Answers 2

1

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;
}
0

I found only the real Node-ID with the referenced webform submission with this code:

$source_entity = $request_handler->getCurrentSourceEntity('webform');
$arrayData = $source_entity->toArray();
$nodeId = intval($arrayData['entity_id'][0]['value']);

$nodeId will be the node where the webform was in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.