2

I am currently setting up a simple LMS in D8 and would like to create different registration procedures for different kinds of users. In particular:

  • There should be a registration page for simple member profiles: Only basic user data should be required and the account should be unlocked immediately after registration (and e-mail confirmation).
  • There should be a second registration page for student profiles: They need to upload a valid student identity card, which is why their registration requires admin approval.

I have already set up different registration profiles/pages for members and students and after registration the correct role is automatically assigned to the new user. So, that is working well already.

However, I need to also set up a different approval process, depending on the registered role and I'm not sure, how to approach this. Note: I really don't want this to be a two-step process, where students register as members first and only then upload the required documents. I need to make this as easy and quick as possible for them, while at the same time allowing for unrestricted member registrations.

Note that I'm working in D8 and I'm hesitant to install the Rules module, as there doesn't seem to be a stable version yet. Or is it usable?

Can this be done? Any help?

  • 1
    This is possible using webform module, are you interested in using webform as your main registration forms? Also no, rules is not stable at all atm – Sohail Oct 28 '18 at 8:07
  • Thanks for the suggestion. Yes, using the webform module would be absolutely okay. I didn't know, webform would allow this? – Marcus C. Oct 28 '18 at 8:14
  • It wouldn't really allow that! but you can use it as you usually would use a webform but then add a custom handler to it to achieve what you are looking for, I'm going to leave an answer but if you provide me with more detail and differences between your roles I can leave a much accurate answer – Sohail Oct 28 '18 at 8:15
  • Thank you very much. Alright, I'll update my question with more information... – Marcus C. Oct 28 '18 at 8:17
  • I'll leave a complete answer by tonight – Sohail Oct 28 '18 at 13:37
1

So, first of all, I'd like to refer to my other answer here, it's good to take a look

You are going to be needing to separate webforms, each with its own required fields, then after building our webform, in Emails \ Handlers section located at admin/structure/webform/manage/[WebformName]/handlers. We are going to enable our custom webform handler module.

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 create our users 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 an example 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('test@example.com');
        $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
        $user->addRole('CustomRoleName');
        //Save user
        $user->save();

  }
}

So based on your use case, you can add the user with a custom role with $user->addRole('CustomRoleName'); and maybe activate it with $user->activate(); it or a lot other things. You will be needing two different handlers/module with different concept for each role, one for simple member profiles and one for student profiles

  • 1
    Thank you very much for your help and your detailed reply. That sounds like it could be a perfect fit to what I had in mind. Will try it out and report back as soon soon as possible. Thanks again. :) – Marcus C. Oct 29 '18 at 15:54
  • Of course man, let me know if you had any issues with it – Sohail Oct 29 '18 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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