Custom Drupal 7 site user registration form

With CAS, it's easy to automatically create Drupal accounts when users access the site for the first time.  Furthermore, Drupal 7 now makes it very easy to add custom form fields to the user account page.

But, what if you want to just show those custom fields and none others?  Well, there's a module for that: Edit profile

Okay, that's great but what if you want users to be able to edit those custom fields but not the default Drupal user account fields, such as email, username, password, etc.?  Well, there's another module for that: Edit own user account permission

Good so far.  Unfortunately, hidden custom fields (i.e. fields on admin/config/people/accounts/fields that are set to "hidden" on the Manage Display tab) are still visible on the Edit Profile tab exposed by the module indicated above.  To hide those fields, an easy fix is to add a display:none CSS property to the theme for those hidden fields.  Note, however, that the user account page uses the administrator theme and not the site theme so in most cases you'll likely need to edit the seven Drupal theme.  I personally like to copy the core seven theme (or whatever administrator theme you have selected for your site) into the site-specific theme folder so my edits don't get overridden during future Drupal core upgrades.  For example:

sites/default/themes/seven/style.css


Great!  So we now have a custom user form displaying only the custom fields we want.  Now, how do we redirect users to that form when they login to the site for the very first time?  Ideally, we'd want to remember their original destination so we can redirect them to it after they fill out our custom user form.  To accomplish all that, we need to create a custom module.  Here's my solution with a custom module called onetime:

onetime.info


onetime.module


Due to the ever-changing intricacies of Drupal's API, this solution took quite awhile to solve (every single line has an important significance).  Hopefully this helps someone else along the way.

P.S. This solution will only apply to new users, not existing users, so you'll need to delete and recreate them manually.

0 comments:

Post a Comment

Keep it clean and professional...