I attempted to create a custom module that automatically creates a node upon submission. I used the second response in this post: How to create a node out of a webform submission?
Per the instructions, I added a php file to a new custom module created through Drupal console. It has an info.yml and .module files.
I took the code from the second comment and altered to suit my module, and add dependencies. (For some reason, the dependencies were incorrect.)
My PtcWebformHandler.php file contents:
<?php
namespace Drupal\ptc_node_from_form\Plugin\WebformHandler;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\Node;
use Drupal\webform\Annotation\WebformHandler;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\Plugin\WebformHandler\EmailWebformHandler;
/**
* Create a new Article node from a webform submission.
*
* @WebformHandler(
* id = "add_new_company",
* label = @Translation("Create a node on submit"),
* category = @Translation("Content"),
* description = @Translation("Creates a new Article 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 PtcWebformHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission)
{
// Get an array of form field values.
$submission_array = $webform_submission->getData();
// Prepare variables for use in the node.
$title = $submission_array['subject'];
// Create the node.
$node = Node::create([
'type' => 'company',
'status' => FALSE,
'title' => $title
]);
$node->save();
}
}
Before enabling the module, I added the email handler (the other post doesn't specifically say to, but it says a handler is required. And the code references an email array.)
After enabling the module, I submited a form, and checked for the node. And... no node.
I'm still getting used to Drupal 8 module development. I tried my hand at creating a service. But I doubt it is necessary (if I understand services). In any regard, it still doesn't create a new node upon form. submission.
I tried to use xdebug to see what the data was being passed to the method inside the PtcWebformHandler class. It didn't hit anything.
What am I missing.
(It worked when I added the router from the first comment in the aforementioned post.)