The answer from @owenpm3 is great for adding this as an administrative duty.
In order to automate this and make it occur on a successful submission, I would create a custom WebformHandler that you add to your form in the configuration settings, the same way you configure your form to send an email upon a successful submission. So under your web forms Settings tab, under the sub-tab: Emails / Handlers.
To build this you would need to create a custom module, mymodule and place the custom webform handler in: mymodule/src/Plugin/WebformHandler/
I will call the WebformHandler MyWebformHandler.php. Aside from this you will need an info file and that should do it.
Code for MyWebformHandler.php as follows:
<?php
namespace Drupal\mymodule\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 Article node from a webform submission.
*
* @WebformHandler(
* id = "article_from_webform",
* 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 MyWebformHandler extends WebformHandlerBase {
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
// Get an array of form field values.
$submission_array = $webform_submission->getData();
// Dump the $submission_array to acquire the fields if you don't know what fields you're working with.
// Prepare variables for use in the node.
$title = $submission_array['subject'];
$body = "<p>" . $submission_array['name'] . "<br/>";
$body .= $submission_array['email'] . "</p>";
$body .= $submission_array['message'];
// Create the node.
$node = Node::create([
'type' => 'article',
'status' => FALSE,
'title' => $title,
'body' => [
'value' => $body,
'format' => 'basic_html',
],
]);
// Save the node.
$node->save();
}
}