I have a need to use Webform (using the Webform CiviCRM module) in Drupal 8 to register a new user and their organisation in Civi and at the same time create the user account in Drupal and link it to the Civi contact. I can't use the standard Drupal register form as I want to include their organisation and other custom fields. How can I do this?
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:
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:
omitting the 'src'.
Once you have created the WebformHandler it should appear on the Webform "Email/Handlers" tab where you can then add it to the form.
Ben's answer sounds like a really good extra tool for this. Just for the record other solutions include
- using Tags and CMS User extension https://github.com/Skvare/com.skvare.cmsuser
- Drupal Rules eg https://github.com/eileenmcnaughton/civicrm_entity/pull/273/files