Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Managing account registration for multiple user types

Parent Feed: 

The Scenario

You're building a new Drupal site that needs to handle two distinct types of users: Consumers and Service Providers. Each user group must have a unique role, profile type, and registration page. Users of each type should be able to visit your site, find the correct registration page, fill out their profile, and be granted an account with the correct role. Sounds easy, right?

Well, I recently found myself in this exact scenario. I was surprised to find that no combination of modules would exactly fit these requirements. This seemed like a great opportunity to build a legitimate, contributed module. Here it is: Profile2 Registration Path.

The purpose of this blog post is to tell a bit of that story, but more importantly, to show you how to do use it!

The Journey

Jump to the Quick Step-by-step (TLDR)

Drupal 7 core provides us with an easy means of adding fields to the user entity. Unfortunately, using the core method requires that we make said fields available to all users. That's not very useful for our current requirements. Enter Profile2.

Designed to be the successor of the core profile module, Profile2 gives us a great starting point. Let's take a quick snippet from the Profile2 project description:

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

Awesome. Now we can create a Consumer profile type and a Service Provider profile type. We're all set, right? Not quite. We need to make sure that our users can easily fill out these profiles during registration, and that they are granted the correct user roles after signing up.

The Problem with Profile2

By default, the Profile 2 module permits you to add fields from each profile type to the default user registration form. Unfortunately, there is only one user registration form. Thus, during registration, every user will be presented with fields from all of the selected profiles. If you have two profile types targeted at two different audiences, you cannot have two separate registration forms. Bummer.

The Solution

Profile2 Registration Path enables you to set a unique registration path for each Profile2 profile type. Users who register via that unique path will be presented with fields from the specified profile type(s), and may have corresponding roles assigned to them upon account creation. Yay.

Here's a quick list of features taken from the Profile2 Registration Path project page:

  • Each registration path can be assigned roles that will be granted to users upon registration
  • Multiple profile types can be assigned to a shared registration path.
  • Profile types can be attached to the core 'user/register' page (while still maintaining other, unique registration paths).

So now that we have all of the tools that we need, how do we put it all together?

The Quick Step-by-step (TLDR)

  1. Download Profile2
  2. Download Profile2 Registration Path
  3. Enable the modules
  4. Create the 'consumer' and 'service provider' roles
  5. Go to admin/structure/profiles and add a new profile type named 'consumer'
  6. Check 'Enable unique registration path'
  7. Enter a URL path to use for this profile type, e.g., 'consumer/register'
  8. Select the role(s) that you would like to apply to users registering from this path. In this case, 'consumer'
  9. Add a few fields to the profile type
  10. Create additional profile types as needed
  11. Configure your permissions so that the correct roles can edit the correct profile types. E.g., only users with role 'consumer' can edit profiles of type 'consumer'

That should do it! You now have a site that can truly support the organic growth of multiple, distinct user groups.

After-thoughts

I initially tried to collaborate with the maintainers of the Auto Assign Role module, which was my go-to Drupal 6 solution for this scenario. I submitted a patch here that would introduce a hook, which would in turn permit the integration of other modules with AAR. Unfortunately, nothing came of it. I still think this is a good idea, so feel free to petition for it!

I'm working on adding more features to Profile2 Registration Path, including:

  • Unique registration blocks
  • Per-profile page titles for the login, registration, and forgot password pages (implemented in dev)

Of course, all feature requests and code contributions are welcome!

Happy coding.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web