Outputting individual CiviCRM profile fields into custom Drupal profile
Assuming you've already modified your template.php file and created a user_profile.tpl.php file as per the suggestions on parent pages of this book, I've coded several ways to pull civiCRM fields into the custom user profile. I'm a bit of an amateur, so please comment if aspects of my coding could be enhanced and I'll happily make revisions:
Code snippet 1: This initializes the civicrm module and defines two handy functions that allow us to retrieve user and profile objects from the civiCRM database using the Drupal user id....I've really only used the first function here (which I found in the civiCRM documentation), but calling the second function during development allows you to print out all of the array keys in the user object if you need to do some digging around.
<?php
if (module_exist('civicrm')) {
civicrm_initialize(TRUE);
function get_user_object($uid) {
civicrm_initialize(true);
$userID = crm_uf_get_match_id($uid);
return crm_get_contact(array('contact_id' => $userID));
}
function Get_Array_Keys_UL($array=array()) {
$recursion=__FUNCTION__;
if (empty($array)) return '';
$out='<ul>'."\n";
foreach ($array as $key => $elem)
$out .= '<li>'.$key.$recursion($elem).'</li>'."\n";
$out .= '</ul>'."\n";
return $out;
}
$userobject = get_user_object($user->uid);
$profileobject = ($userobject->contact_type_object);
}
?>Code snippet 2: This uses $profileobject object to return standard civiCRM field types such as first_name, last_name, and job_title
<?php print ($profileobject->first_name).' '.($profileobject->last_name).'<br>'; ?>
<?php print ($profileobject->job_title); ?>Code snippet 3: I was having a dickens of a time getting at the custom fields I created as a part of the civiCRM user profile, since they appear to be keyed differently for each user, and they are sometimes serial lists separated by the mysterious . character .
They do each have a field_id associated with them, and you can find the field_id of each custom field you defined by poking around your CRM tables. This code iterates through all custom data values an outputs only those associated with field_id == 9. You can change this and add additional
<?php $custom_fields = $userobject->custom_values ?>
<?php
$i = 0;
foreach ($custom_fields as $custom_field){
$field_id = $custom_fields[$i]['custom_field_id'];
$fieldvalues = split('', $custom_fields[$i]['char_data']);
$i++;
foreach ($fieldvalues as $fieldvalue){
//you will need to change the field_id values below to reflect the field_ids you want
if ($field_id == 9){
//you will need to theme the fields yourself
print '<div class="fieldset">'.$fieldvalue."</div>";
}
if ($field_id == 5){
//you will need to theme the fields yourself
print '<div class="fieldset">'.$fieldvalue."</div>";
}
//place additional if clauses here for additional fields
}
}
//Uncommenting the line below during development will allow you to see all the arrays and objects inside the custom_values object.
//print_r ($custom_fields);
?>My guess this that list bit probably could be done much more elegantly. Please offer suggestions...

Drupal 5+ Change
For Drupal 5+ compatibility, 'module_exist' has become 'module_exists'. You'll need to change line 2 in the first snippet so that it reads:
if (module_exists('civicrm')) { //note the 's' on 'exists'Otherwise it works like a charm. Thanks man.
civicrm v2.0 API
For those who are upgrading CIVICRM:
doesn't work with the civicrm v2 API (yet)
to print standard fields CIVICRM 2.0
Figured out how to show the standard values from a contact. Will look at the custom fields later this week.
<?php
if (module_exists('civicrm')) {
//initialize civicrm
civicrm_initialize(TRUE);
//required to use crm_uf_get_match_id function
require_once 'api/UFGroup.php';
//get user id from CIVICRM, using the $uid from the contact you're looking at
$userID = crm_uf_get_match_id( $user->uid );
// We will find the values from this contact using it's userID
$retrieve = array( contact_id => $userID );
//required to use civicrm_contact_get function
require_once 'api/v2/Contact.php';
$getContact = civicrm_contact_get( $retrieve );
//remove comments from line when you want to take a look at the whole array
//print_r($getContact);
?>
Now we can display all the necessary fields, like display_name, and mix it with drupal functions like showing user_picture.
<?phpif($user->picture) {
print theme('user_picture', $user);
}
echo $getContact['display_name'];
//end of: if module_exists
}
?>