How to use webform to register a new user on Drupal 8/9 and link to contact in CiviCRM
Using Webform and Webform CiviCRM modules create a webform with the fields required for the user and their organisation to create the contact and organisation in CiviCRM.
Then create a WebformHandler to programmatically create the user using the postSave() function. The WebformHandler is a single php file that needs to be saved in a custom module folder as:
my_module/src/Plugin/WebformHandler/CreateUserHandler.php
I have created a basic guideline WebformHandler that creates the user and connects to the CiviContact at https://gist.github.com/bmango/0347f6c22b3fb38e34be57bb50f8e99e
Note that the namespace declaration at the top of the script is not the same as the path to the file but will be:
namespace Drupal\my_module\Plugin\WebformHandler;
omitting the 'src'.
Once you have created the WebformHandler it should appear on the Webform &quot;Email/Handlers&quot; tab where you can then add it to the form.
This answer extends the answer given by mAtZ to Create a new user on Webform submission, by adding the code to connect the Drupal user account to the Civi contact.