Score:0

Why isn't this working?

cn flag

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();
  }
}```
id flag
The question does not say in what way this code is not working.
gmak avatar
cn flag
My apologies, the sensor is submitting successfully but the nodes are not created. There are no errors being logged.
4uk4 avatar
cn flag
Did you add the handler to a webform?
gmak avatar
cn flag
My handler is not showing up in the list of handlers for the webform.
gmak avatar
cn flag
Fixed: I wasn't paying attention to the metadata at the start of @WeformHandler, which gives the label of the Handler that will appear in the webform. Now that I've recognised that, it is working!!!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.