Community Documentation

Outputting individual CiviCRM profile fields into custom Drupal profile

Last updated August 26, 2009. Created by Andrupal on April 5, 2007.
Edited by ronald_istos. Log in to edit this page.

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

Comments

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.

<?php
 
if($user->picture) {
print
theme('user_picture', $user);
}
echo
$getContact['display_name'];
//end of: if module_exists
}
?>

About this page

Drupal version
Drupal 4.7.x
Audience
Themers

Theming Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.