CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Create Drupal user when adding new contact
Pages: [1]

Author Topic: Create Drupal user when adding new contact  (Read 1059 times)

yaunus

  • I’m new here
  • *
  • Posts: 13
  • Karma: 0
Create Drupal user when adding new contact
October 01, 2010, 04:29:44 pm
I'm trying to configure CiviCRM to add a new Drupal user anytime a Contact is created. I've created two profiles forms, which, when filled out by an anonymous user, create a new Drupal user. But if the administrator fills out the form, no new Drupal user is added.

How would I go about configuring this?

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Create Drupal user when adding new contact
October 03, 2010, 10:02:01 am
1. "out of the box" (no coding required) solution -> admins need to log out and fill in the profile form as anonymous

2. some custom code: implement the postProcess hook in a custom module and use drupal api functions to create the user whenever the back office "add contact" form is submitted (this will require some PHP skills) - check the Extending and Customizing section of the online book for an intro to this approach:

http://en.flossmanuals.net/CiviCRM/DevelopIntro
Protect your investment in CiviCRM by  becoming a Member!

yaunus

  • I’m new here
  • *
  • Posts: 13
  • Karma: 0
Re: Create Drupal user when adding new contact
October 04, 2010, 02:54:08 pm
Thanks, I've gone ahead with the custom code method. Here's what I have so far:

Code: [Select]
function hook_civicrm_pre( $op, $objectName, $objectId, &$objectRef ) {
       
    if ($objectName == 'Individual' && $op == 'create') {
        $roles = user_roles();
        $username = strtolower($objectRef->first_name . $objectRef->last_name);
       
        if ($error = user_validate_name($username)) {
          form_set_error('name', $error);
          return;
        }

        $user = array(
            'name' => $username,
            'email' => $objectRef->email[0]->email,
            'status' => 1,
            'pass' => user_password()
        );       
       
       
        if ($objectRef->contact_sub_type == 'Fellow') {
            $user['roles'] = array(FELLOW_ROLE => $roles[FELLOW_ROLE]);
       
        } else if ($objectRef['contact_sub_type'] == 'Sponsor') {
            $user['roles'] = array(SPONSOR_ROLE => $roles[SPONSOR_ROLE]);
            $user['roles'] = array('sponsor');
        }
        $user = user_save(null, $user);
        if (!$user) {
            drupal_set_message('Failed to create a new Drupal user. A user with the same first and last name may already exist.', 'warning');
        }
    }
}

The problem I'm facing now is, I'd like to stop the form from submitting if the user does not save. Is there a function or something I need to call to stop the profile from being created?

Thanks!

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Create Drupal user when adding new contact
October 04, 2010, 06:02:16 pm
Seems like you should use $post - since you want to only create a (Drupal) user after a (CiviCRM) contact is inserted in the DB. Might be easier to jump on IRC (#civicrm) and discuss.
Protect your investment in CiviCRM by  becoming a Member!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Create Drupal user when adding new contact

This forum was archived on 2017-11-26.