I'm trying to take the data from a webform submission and create 2 nodes. I've looked at various examples, but I cannot figure out why this isn't working. Can anyone help? Thanks in advance.
My file structure looks like:
srs
srs_webform.info.yml
srs_webform.module
src
Plugin
WebformHandler
SRS_WebformHandler.php
//srs_webform.info.yml
name: SRS Webform
type: module
description: Create Nodes on webform submission.
package: SRS
core_version_requirement: ^9 || ^10
dependencies:
- webform:webform
function srs_webform_theme() {
return [
'webform_handler_srs_webform_summary' => [
'variables' => [
'settings' => NULL,
'handler' => [],
],
],
];
}
//SRS_WebformHandler.php
namespace Drupal\srs_webform\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;
/**
* Create a new node entity from a webform submission.
*
* @WebformHandler(
* id = "Create a node",
* label = @Translation("Create a node"),
* category = @Translation("Entity Creation"),
* description = @Translation("Creates a new node from Webform Submissions."),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_REQUIRED,
* )
*/
class SRS_WebformHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
// Create node object from webform-submission.
// 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.
$form_title = $webform->label();
//if ($form_title = "Enquiry"){
$values = $webform_submission->getData();
// First create a Person node
$node = Node::create([
'type' => 'person',
// Use this format: 'node_machine_name_field' => $values('webform_machine_name_field') for below fields.
'field_firstname' => $values['first_name'],
'field_surname' => $values['surname'],
'field_email_address' => $values['email_address'],
'title' => $values['surname'].", ".$values['first_name']
]);
// Save the Person node
$node->save();
// Store the nid of the just saved Person node
$person_id = $node->nid;
// Now create the Enquiry node
$node = Node::create([
'type' => 'enquiry',
'field_lead_type' => $values['enquiry_type'],
'field_query' => $values['query'],
'field_course' => $values['course'],
'field_person' => $person_id,
'title' => $values['surname'].", ".$values['first_name']." - ".$values['enquiry_type']
]);
// Save the Enquiry node
$node->save();
}
}```