So as I mentioned in my comment, the proper way to achieve this is to use the Handlers
functionality in Webform, for that we need a custom module for our Handler
to show up in admin/structure/webform/manage/[WebformName]/handlers
One thing that you need to make sure is correct, is the structure of the module folder, so that webform would read it as a handler
, we assume the module name here is: webform_handler_user_creation
so the structure would be something like this:
webform_handler_user_creation>src>Plugin>WebformHandler>UserCreationHandler.php
with a webform_handler_user_creation.info.yml
file in the root of the module directory.
With all that said, here's an example of the module on my github page
So what we need to do first is to make sure we do our thing after saving the webform submission, then load the submission values and use them based on their machine names to fill in our user creation thingy, here's the code:
<?php
namespace Drupal\webform_handler_user_creation\Plugin\WebformHandler;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\Entity\WebformSubmission;
/**
* Webform submission action handler.
*
* @WebformHandler(
* id = "usercreate",
* label = @Translation("User Create"),
* category = @Translation("Action"),
* description = @Translation("Creates a user after submission"),
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL,
* )
*/
class UserCreationHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
$user = \Drupal\user\Entity\User::create();
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$user_name = $webform_submission->getElementData('user_name');
// Mandatory user creation settings
$user->enforceIsNew();
$user->setPassword('testPassword');
$user->setEmail('[email protected]');
$user->setUsername($user_name); // This username must be unique and accept only a-Z,0-9, - _ @ .
$user->set("langcode", $language);
// Optional settings
$user->set("init", 'email');
$user->set("preferred_langcode", $language);
$user->set("preferred_admin_langcode", $language);
$user->activate();
// Add a custom role in case you need one
$user->addRole('CustomRoleName');
//Save user
$user->save();
}
}
also note the namespace at the beginning of the file, this is where it gets loaded into the administration area of a webform like the following image:

Note that in this example we use $user_name
as the only field that gets populated dynamically from the webform submission, you have to replace the rest according to your webform fields machine names
Handlers
in webform settings itself. you have to create a custom handler for saving a new user and use the webform's field data to achieve that, if you couldn't figure it out yourself, I'll leave an answerHandlers
settings page it's not obvious how I can create a user. It looks like I can just send an email or post form data to a URL. Does this require some custom code to achieve? A google search seems to suggest this.