Here is what I need to do:

The user uploads an image, and I need to change the filename of the image to be firstname_lastname.filtype.

The first and lastname are stored within CiviCRM, but the upload is occuring in Drupal 7.

I'm thinking I need to write some PHP that pulls in the data from CiviCRM, and then sets it as the file name.

I'm still pretty new to Civi, can anyone give me some guidance on how I could go about doing this? How can I pull in the data from CiviCRM?

Or, is there a way to use CiviCRM tokens inside of Drupal?

  • Where is the image uploaded on Drupal 7 ? (eg. Is it part of a webform?) – JoAnne May 2 '17 at 9:56

I am assuming that you are a drupal coder (because the upload starts in Drupal). Information from CiviCRM can be used in Drupal by using the CiviCRM. This is documented in the Developer Guide.

An example for finding the first and last name of the current user in drupal is

$uid = $GLOBALS['user']->uid;   // get id current user

civicrm_initialize(); // needed for classloading

/* find the CiviCRM contact id using the drupal user
$contact_id = civicrm_api3('UFMatch', 'getvalue', array(
  'return' => "contact_id",
  'id' => $uid,

/* use contact id to find contact details */
$result = civicrm_api3('Contact', 'getsingle', array(
  'id' => $contact_id,

/* create the file name */
  • 2
    To explore the civicrm API further, check out the API Explorer under the Support menu in CiviCRM. – Chris Burgess May 2 '17 at 10:22
  • I just tried that code, and it is saying that it is returning 25 contacts, but of course expects 1 – Kimberlie Davis May 2 '17 at 22:37
  • Oka, I figured it out. What I did was used the Custom Tokens module and the code you provided to create custom tokens for the CiviCRM data. Thanks for your help! – Kimberlie Davis May 3 '17 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.