8

I'm looking to create a user registration form on a Drupal 8 site that has multiple clearly defined sections (possibly on separate pages).

I've explored doing this with the standard Drupal registration form building tools in account settings but it doesn't seem possible to create a form with such a layout using just account settings (correct me if I'm wrong).

The webform module allows me to create a form which is styled exactly how I want but I'm not sure how to create a user account with data entered into a webform. Is there another module I can use for this or can I do it from within the webform module?

4
  • I've already done this before in a project a while back, you should be able to do this using 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 answer
    – Sohail
    Oct 15, 2018 at 18:38
  • 1
    @Sohail Would be great if you could post an answer. Looking at the Handlers 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. Oct 22, 2018 at 8:47
  • I'll try to see if I can get a working handler for this till tomorrow
    – Sohail
    Oct 23, 2018 at 15:06
  • I updated the answer, please take a look and see if it works
    – Sohail
    Oct 24, 2018 at 15:19

5 Answers 5

2

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

3
  • Thanks for your help so far, this makes it clearer. I'll accept your answer as it has gotten me closest to what I want to do so far. I am however still encountering an issue. I've created a module called researcher_reg in the way you've described however when I go to add the handler to the webform after installing the module the handler doesn't show up. The namespace line in my UserCreationHandler.php file is namespace Drupal\researcher_reg\Plugin\WebformHandler;. Any ideas how I can fix this? Thanks. Oct 29, 2018 at 10:59
  • Thanks for accepting the answer, and Yes the reason that it doesn't show up is that you probably didn't name it! You see that commented section with @WebformHandler? Yeah, that's not really a comment =P, make sure to have it there and clear cache afterwards
    – Sohail
    Oct 29, 2018 at 23:27
  • Thanks, it worked for me as well. For Password : $user->setPassword($pass_word); To make the password drupal specific.
    – Wasim Khan
    Mar 27, 2019 at 5:37
3

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.
3
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.

2
  • Looks like it could work although there is no stable release available. Do you know of an alternative that does the same thing? Oct 15, 2018 at 8:13
  • As of June 2020 there is a stable release with version 1.4
    – yunzen
    Jun 18, 2020 at 8:35
0

As stated, if possible, you'll have a much easier time using the standard Drupal registration process, rather than trying to create a webform and sync fields with the registration form.

Depending on your site needs / restrictions, you could always "fake" a multi-step form with a custom template. If all of the form fields print out on the same page, you can use the template to split those fields into "sections" and then use JS to let the user step through the full process while staying on the same page.

A great example is provided here - https://www.w3schools.com/howto/howto_js_form_steps.asp - which also includes form validation on the form step buttons, so that users can't skip a required field on Step 1 before moving on to Step 2.

2
0

I was able achieve this on multiple Drupal 8 site and could create users on both Drupal & civicrm. I used some custom code to create user in Drupal by taking the submitted values.

Here is a thread but it is old and may have different approach with Drupal 8.6.x

https://www.drupal.org/forum/support/module-development-and-code-questions/2015-03-04/how-to-programmatically-create-a-user

This is about creating users in code after the webform has been submitted. For template, check this: https://www.drupal.org/docs/8/theming/twig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.