0

I have a webform that is connected to a node in drupal 8. When the webform loads from the node link I need to get the source entity id from url. How can I achieve that in my custom module? the url is as follows: https://example.com/en/form/edu-form?source_entity_type=node&source_entity_id=134

I tried the following but I get an error:

 $node = \Drupal::routeMatch()->getParameter('source_entity_id');
 $nodeId = $node->id();

And also this (I tried to get the node id from the webform object)

/** @var \Drupal\webform\WebformRequestInterface $request_handler */
$request_handler = \Drupal::service('webform.request');
$source_entity = $request_handler->getCurrentSourceEntity('webform');
$node_id=$source_entity->id;
  • You set $node equal to the source_entity_id and then try to access it like it's an object, but it's just an id. Also, you should include your error message. – sonfd Apr 2 '19 at 22:42
0

Make sure that the webform's source entity id can be prepopulated via a URL and the below code should work using $source_entity->id() instead of $source_entity->id

/** @var \Drupal\webform\WebformRequestInterface $request_handler */
$request_handler = \Drupal::service('webform.request');
$source_entity = $request_handler->getCurrentSourceEntity('webform');
$node_id = $source_entity->id();

enter image description here

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.