0

I have looked elsewhere for the problem I faced, but I cannot get any clue on solving the problem. Consider, based on the type of user, I want to implement different user registration page. In current D8 version, I could add fields at admin/config/people/accounts/fields. Let's say a teacher has fields like Fname, Lname, ID, Contact, and School Name whereas student has fields like Fname, Lname, Course, and Age.

If I add those mentioned fields in the user table, I won't be able to differentiate whether that is teacher or student. In that case, how do I make two different registrations, one for teacher another for student, and store them as Drupal users so that I can apply functions such as \Drupal::currentUser->isAuthenticated() on them?

3 Answers 3

6

I would argue that you only need one type of user, but want multiple types of profiles. It sounds like you are looking for the profile module. From the module description:

Why use profiles instead of user account fields?

  • With profile, user account settings and user profiles are conceptually different things, e.g. with the “Profile” module enabled users get two separate menu links “My account” and “My profile”.
  • Profile allows for creating multiple profile types, which may be assigned to roles via permissions (e.g. a general profile + a customer profile)
  • Profile supports private profile fields, which are only shown to the user owning the profile and to administrators.

It also supports multiple registration forms to allow users to register and create their profile in one form.

https://www.drupal.org/project/profile

2
  • How should I approach if I were not to use the profile module?
    – Mahadeva
    Commented Oct 26, 2016 at 21:06
  • You would probably have to look at a custom implementation of the User entity class. It will be a significant undertaking.
    – Chance G
    Commented Oct 26, 2016 at 21:14
3

In addition to the Profile module, there's another contributed module written for this: https://www.drupal.org/project/multiple_registration

If you don't want to rely on a module, you can use the Form API to create your own registration form. Or you can use hook_form_alter to alter the registration form beforehand. You could use a query string to determine which kind of form it is, such as /user/register?type=teacher or /user/register?type=student.

3

In D8 you have the new feature form modes.

When you have added the custom user fields go to "Manage form display". At the bottom of the page enable the form mode "Register". Then you get a new tab at the top of the page and you are able to choose the fields for the custom register form.

If you install the module Field Group, you can group the two sets of fields for teachers and students in tabs.

6
  • It's clear for creating different form display, but what about how using those form display??
    – Asy
    Commented Jan 2, 2017 at 11:29
  • @Asy: This is defined in the route. See user.routing.yml. The route user.register defines the defaults _entity_form: 'user.register'. The second part of this option is the form mode.
    – 4uk4
    Commented Jan 2, 2017 at 12:29
  • ok but I should change in user.routing.yml directly??
    – Asy
    Commented Jan 2, 2017 at 14:42
  • No, use a route subscriber to change core routes. But the register form mode is already set up. No need to do anything. After you have customized it, you can use it right away.
    – 4uk4
    Commented Jan 2, 2017 at 14:49
  • yes but I'm talking about a new form mode that I could use for another user role. I really don't get how to do it , sorry :(
    – Asy
    Commented Jan 2, 2017 at 15:19

Your Answer

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

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