Drupal - Create a webform which creates a user account

There is a module for that.

Forms Steps provides an UI to create forms workflows using forms modes. It creates quick and configurable multisteps forms.


Welcome to Drupal Answers!

If styling is the only concern here, I would strongly recommend not using webform over Drupal's default registration process. You could simply add more fields to the user from account settings and have them displayed while registration. Benefits:

  1. The data remains attached to the user entity even after registration, which means the users' will have the facility to edit the values provided while registration.
  2. You could use them with ease to create the profile pages if intended.
  3. You could use these fields as filters on the users listing page.
  4. Many other cases depending on the site.

As for theming the form, I did a quick search and found some useful links:

  1. Drupal 8: How to theme any form.
  2. Send a form to twig template.
  3. How do I theme a custom form?
  4. Theming form elements in Drupal 8.

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: enter image description here

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

Tags:

Webforms

Users