CiviCRM: retrieve CiviCRM Contact from Drupal uid

A first attempt at some code code using the wonderful CiviCRM API version 3.

I’m using Drupal 6, and want to retrieve details from CiviCRM relating to the logged in Drupal user.

<?php

/**
 * Implements hook_user().
 */
function civicrm_login_message_user($op, &$edit, &$account, $category = NULL) {
  if ( ! civicrm_initialize( ) ) {
    return;
  }  
  switch ($op) {
    case 'login':
      global $civicrm_root;
      require_once $civicrm_root . '/api/api.php';

      $uid = $account->uid;

      // In CiviCRM, 'uf' means 'user framework'.
      // A user framework can be Drupal, Joomla, WordPress etc.
      //
      // See note on 'sequential' parameter in the API documentation:
      // http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs

      // Without Sequential:
      $params = array(
        'uf_id' => $uid,
        'version' => 3,
      );

      $UFMatch = civicrm_api( 'UFMatch','Get',$params );
      drupal_set_message('<pre>' . print_r($UFMatch, TRUE) . '</pre>');

      $UFMatch_id = $UFMatch['id'];
      $contact_id = $UFMatch['values'][$UFMatch_id]['contact_id'];
      drupal_set_message($contact_id);

      // Using Sequential, the values array is keyed sequentially.
      // This means, we can know that the first key in the array is 0.
      $params = array(
        'uf_id' => $uid,
        'version' => 3,
        'sequential' => 1,
      );

      $UFMatch = civicrm_api( 'UFMatch','Get',$params );
      drupal_set_message('<pre>' . print_r($UFMatch, TRUE) . '</pre>');

      $contact_id = $UFMatch['values'][0]['contact_id'];
      drupal_set_message($contact_id);

      // Now we know the CiviCRM contact_id, we can use it to make new calls to
      // the CiviCRM API version 3.
      $params = array(
        'contact_id' => $contact_id,
        'version' => 3,
      );

      // Get Contact details for the user.
      $contact = civicrm_api( 'contact','get',$params );
      drupal_set_message('<pre>' . print_r($contact, TRUE) . '</pre>');

      // Get Membership details for the user.
      $membership = civicrm_api( 'membership','get',$params );
      drupal_set_message('<pre>' . print_r($membership, TRUE) . '</pre>');

      break;
  }
}

Here is some example output from this code:

  • Array
    (
        [is_error] => 0
        [version] => 3
        [count] => 1
        [id] => 2
        [values] => Array
            (
                [2] => Array
                    (
                        [id] => 2
                        [domain_id] => 1
                        [uf_id] => 3
                        [uf_name] => example.person@example.org
                        [contact_id] => 103
                    )
    
            )
    
    )
  • 103
  • Array
    (
        [is_error] => 0
        [version] => 3
        [count] => 1
        [id] => 2
        [values] => Array
            (
                [0] => Array
                    (
                        [id] => 2
                        [domain_id] => 1
                        [uf_id] => 3
                        [uf_name] => example.person@example.org
                        [contact_id] => 103
                    )
    
            )
    
    )
  • 103
  • Array
    (
        [is_error] => 0
        [version] => 3
        [count] => 1
        [id] => 103
        [values] => Array
            (
                [103] => Array
                    (
                        [contact_id] => 103
                        [contact_type] => Individual
                        [sort_name] => Person, Example
                        [display_name] => Example Person
                        [do_not_email] => 0
                        [do_not_phone] => 0
                        [do_not_mail] => 0
                        [do_not_sms] => 0
                        [do_not_trade] => 0
                        [is_opt_out] => 0
                        [preferred_mail_format] => Both
                        [first_name] => Example
                        [last_name] => Person
                        [is_deceased] => 0
                        [contact_is_deleted] => 0
                        [address_id] => 90
                        [street_address] => 1 Example Street
                        [city] => Example City
                        [email_id] => 92
                        [email] => example.person@example.org
                        [on_hold] => 0
                        [id] => 103
                    )
    
            )
    
    )
  • Array
    (
        [is_error] => 0
        [version] => 3
        [count] => 1
        [id] => 31
        [values] => Array
            (
                [31] => Array
                    (
                        [id] => 31
                        [membership_id] => 31
                        [contact_id] => 103
                        [membership_contact_id] => 103
                        [membership_type_id] => 1
                        [join_date] => 2009-11-01
                        [start_date] => 2009-11-01
                        [membership_start_date] => 2009-11-01
                        [end_date] => 2011-10-31
                        [membership_end_date] => 2011-10-31
                        [status_id] => 3
                        [reminder_date] => 2011-09-30
                        [is_test] => 0
                        [member_is_test] => 0
                        [is_pay_later] => 0
                        [member_is_pay_later] => 0
                        [membership_name] => General
                        [relationship_name] => Household Member of
                    )
    
            )
    
    )
Advertisements
Posted in CiviCRM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: