Last updated February 24, 2012. Created by scarer on February 9, 2009.
Edited by snevins, figaro. Log in to edit this page.

This post details how to add registration fields that assign a user to a role when they register so this role can be mapped to CiviCRM groups. To follow the post, you will need to have Drupal 6 installed and the Rules module, the CiviCRM module, and the Profile module enabled.

Follow the steps below to achieve the aforementioned task.

  1. Add checkbox profile fields to the user profiles representing the roles in Drupal that you want to map the user to. Make sure these fields are not visible in any of the displays to other users beside admin and the user. Categorise these fields appropriately (i.e. User subscriptions). Make sure the fields are visible in the registration form. Do not make the fields mandatory.
  2. Next create the roles in Drupal that you want to map the users to.
  3. Now create the user groups in CiviCRM that you want to map the Drupal roles to.
  4. Synchronise the Drupal roles with the CiviCRM groups through the administration area.
  5. Add a new triggered rule that occurs on the event 'User account has been created'.
  6. Add a condition to the triggered rule 'Execute custom PHP code'. (The PHP Filter module must be enabled for 'Execute custom PHP code' to appear as an option)
  7. Add this code in accordance with the values in your database.
  8. //registered user's uid
    $userUid = $account->uid;
    //field fid for desired checkbox in profile_fields table
    $roleFid = 4;
    $result = db_query("SELECT profile_values.value FROM profile_values WHERE profile_values.uid = $userUid AND profile_values.fid = $roleFid");
    while ($node = db_fetch_object($result)) {
       $value = $node->value;
       if ($value == 1)
       {
          return true;
       }
       else
       {
          return false;
       }
    }//end while statement
  9. Add the action 'Add user role'.
  10. Select 'registered user' in the drop down and pick the role you want to assign them to according to the profile field value.

@todo: Labels parameters

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.