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?
2 Answers
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 "Email/Handlers" 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.
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
-
1
-
So the workflow with the skvar extension is: Webform to create Contact with Group X or Tag Y --> chron to execute CMSUser --> CMSUser scans for Group-X or Tag-Y contacts and creates Drupal user then clears the X and Y so as to not repeat it. Is that right?– HeneryHApr 29, 2021 at 14:15