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) »
  • Drupal - Fetching user first and last names from civi profile
Pages: [1]

Author Topic: Drupal - Fetching user first and last names from civi profile  (Read 1070 times)

felixmartel

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Drupal 6.31
  • MySQL version: 5.1.54
  • PHP version: 5.3.5
Drupal - Fetching user first and last names from civi profile
October 29, 2011, 12:53:30 pm
Hello,

I'm pretty new with CiviCRM and Drupal. I'm setting up a site for a non-profit, using Drupal 6 and Civi 3.4. I'm using Civi profiles for user registration. In Drupal, I want to display the user's full name rather than their user name in places such as author pane (for forums, blogs and comments), and in places such as the logged-in block and users online block. I don't have Drupal profile fields for first and last names, but I'm displaying the civi profile in the user profile page using views. I've got the Real Names module installed as well, but it's designed to work with User Profiles and the APK, so I'm not sure it's of any use here...

What is the best way to fetch the information from civi? Keep in mind I'm not very good at php and still learning my way around Drupal and CiviCRM, so talk slow... :-)

Any help is much appreciated.

Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Drupal - Fetching user first and last names from civi profile
October 30, 2011, 04:15:48 am
API: http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+Public+APIs

Probably PHP method is what you need. Check out the API Explorer to test it here: http://drupal.demo.civicrm.org/civicrm/api/doc or that URL on your own site.
CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

FatherShawn

  • Ask me questions
  • ****
  • Posts: 372
  • Karma: 25
    • C3 Design
  • CiviCRM version: 4.2.11
  • CMS version: Drupal 7.23
  • MySQL version: 5.5.32
  • PHP version: 5.3.10
Re: Drupal - Fetching user first and last names from civi profile
October 31, 2011, 05:12:39 am
Here's some code that I used to do this on a D6 site - others may have a more efficient approach.  You will need to put this in your theme's template.php and replace "YOURTHEME" with the name of your theme...

Code: [Select]
/*
 *Override_function to replace username with First and Last Name pulled from Civi.
*/
function YOURTHEME_username($object) {

  /* If there is a uid and a name, then there should be a CiviCRM record */
  if ($object->uid && $object->name) {
    civicrm_initialize();
    require_once('api/v2/Contact.php');
    require_once('api/v2/UFGroup.php');
    $usernameCiviID = civicrm_uf_match_id_get($object->uid);
 
    $params = array(
                    'contact_id'          => $usernameCiviID,
                    'return.display_name' => 1,
                    );
    $userCiviLookup = civicrm_contact_search( $params );
    $userDisplayName = $userCiviLookup[$usernameCiviID]['display_name'];
    /* Check for error */
    if ( civicrm_error ( $userCiviLookup ) ) {
      drupal_set_message($userDisplayName['error_message'], $type = 'error');
      $userDisplayName = $object->name;
      }

    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($userDisplayName) > 20) {
      $userDisplayName = drupal_substr($userDisplayName, 0, 15) .'...';
    }

    if (user_access('access user profiles')) {
      /*
       * Drupal 6 link function: l($text, $path, $options = array())
       */
      $output = l($userDisplayName, 'user/'. $object->uid, array('absolute' => TRUE,'attributes' => array('title' => t('View user profile.'))));
    }
    else {
      $output = check_plain($userDisplayName);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if (!empty($object->homepage)) {
      $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = check_plain(variable_get('anonymous', t('Anonymous')));
  }

  return $output;
}
Lead Developer, C3 Design.
Twitter: @FatherShawn

felixmartel

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Drupal 6.31
  • MySQL version: 5.1.54
  • PHP version: 5.3.5
Re: Drupal - Fetching user first and last names from civi profile
October 31, 2011, 10:15:27 am
Thanks for the code snip, FatherShawn. I tried adding it to my template.php (and changed YOURTHEME to my theme name). No joy. How can I troubleshoot this ? My php skills are, to say the least, rudimentary. Is there anything I could look for using Firebug or something ? I'm not getting any error messages, but there's no impact on my displayed names.

Thanks !

felixmartel

  • I post occasionally
  • **
  • Posts: 47
  • Karma: 0
  • CiviCRM version: 3.4.7
  • CMS version: Drupal 6.31
  • MySQL version: 5.1.54
  • PHP version: 5.3.5
[solved]Re: Drupal - Fetching user first and last names from civi profile
November 06, 2011, 07:29:46 am
Problem fixed. I had the Real Names module enabled...

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Drupal - Fetching user first and last names from civi profile

This forum was archived on 2017-11-26.