Index: profile_role.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/profile_role/profile_role.module,v
retrieving revision 1.4
diff -u -r1.4 profile_role.module
--- profile_role.module	23 Jan 2009 20:11:43 -0000	1.4
+++ profile_role.module	15 Feb 2009 03:07:20 -0000
@@ -68,6 +68,42 @@
 }
 
 /**
+ * Restrict the user registration form.
+ */
+function profile_role_form_user_register_alter(&$form, $form_state) {
+  if ($form['#action'] == '/user/register') {
+    // No way to determine what role user will be, so leave form alone.
+    return;
+  }
+
+  $roles = isset($form_state['post']['roles']) ? $form_state['post']['roles'] : array();
+  $category_roles = profile_role_get_roles();
+  $result = db_query('SELECT DISTINCT(category)
+                      FROM {profile_fields}');
+  while ($category = db_result($result)) {
+    if (isset($form[$category])) {
+      // Category displayed, check to see if it should.
+      $remove = TRUE;
+
+      if (isset($category_roles[$category])) {
+        foreach ($category_roles[$category] as $rid) {
+          if (array_key_exists($rid, $roles)) {
+            $remove = FALSE;
+          }
+        }
+      }
+
+      if ($remove) {
+        unset($form[$category]);
+      }
+    }
+  }
+
+  // Submit the form so that any additional fields display.
+  $form['account']['roles']['#attributes']['onchange'] = 'submit()';
+}
+
+/**
  * Define profile categories by role.
  */
 function profile_role_restrict_form(&$form_state) {
