Answers.Body: <p>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 <a href="https://docs.civicrm.org/dev/en/latest/api/general/" rel="nofollow noreferrer">Developer Guide</a>.</p>
  
  <p>An example for finding the first and last name of the current user in drupal is</p>
  
  <pre><code>$uid = $GLOBALS['user']-&gt;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' =&gt; "contact_id",
    'id' =&gt; $uid,
  ));
  
  /* use contact id to find contact details */
  $result = civicrm_api3('Contact', 'getsingle', array(
    'id' =&gt; $contact_id,
  ));
  
  /* create the file name */
  $file_name=$result['first_name'].'_'.$result['last_name'];   
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-02T10:22:20.427Z
Answers.Text: To explore the civicrm API further, check out the API Explorer under the Support menu in CiviCRM.
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-02T22:37:34.847Z
Answers.Text: I just tried that code, and it is saying that it is returning 25 contacts, but of course expects 1
Answers.UserDisplayName: Kimberlie Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-03T03:13:16.463Z
Answers.Text: 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!
Answers.UserDisplayName: Kimberlie Davis
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-02T08:42:10.520Z
Answers.LastActivityDate: 2017-05-02T08:42:10.520Z
Answers.UserDisplayName: Kainuk - CiviCooP
Body: <p>Here is what I need to do:</p>
  
  <p>The user uploads an image, and I need to change the filename of the image to be firstname_lastname.filtype.</p>
  
  <p>The first and lastname are stored within CiviCRM, but the upload is occuring in Drupal 7.</p>
  
  <p>I'm thinking I need to write some PHP that pulls in the data from CiviCRM, and then sets it as the file name.</p>
  
  <p>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?</p>
  
  <p>Or, is there a way to use CiviCRM tokens inside of Drupal?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-05-02T09:56:38.077Z
Comments.Text: Where is the image uploaded on Drupal 7 ? (eg. Is it part of a webform?)
Comments.UserDisplayName: JoAnne
Tags: <drupal><api><php>
Title: Using data from CiviCRM to change filename
ThreadText: # Using data from CiviCRM to change filename
  
  (Asked by: username_0 on 2017-05-02)
  
  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?
  
  **Tags:** drupal, api, php
  
  > **Comments:**
  > **username_1**: Where is the image uploaded on Drupal 7 ? (eg. Is it part of a webform?)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-05-02. Score: 2
  
  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 */
  $file_name=$result['first_name'].'_'.$result['last_name'];
  
  > **Comments:**
  > **username_3**: To explore the civicrm API further, check out the API Explorer under the Support menu in CiviCRM.  
  > **username_0**: I just tried that code, and it is saying that it is returning 25 contacts, but of course expects 1  
  > **username_0**: 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!  
  
  ---
  
  