Answers.Body: <p>I was able to figure this out using the following code:</p>
  
  <pre><code>        $getSessionArray = CRM_Core_Session::singleton()-&gt;get('CRM_Utils_Recent');
          $thisContactID = $getSessionArray[0]['id'];
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-10T22:08:00.080Z
Answers.Text: Great. Just a note for others that if you remove Contacts from the list at Administer - System Settings - Misc where it says "Recent Items Providers" then this might fail, but I'd guess that very few sites do that.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-10T15:31:39.200Z
Answers.LastActivityDate: 2019-12-10T15:31:39.200Z
Answers.UserDisplayName: Moe
Body: <p>Been stuck on this for the past few days I want to set a default value for a custom field in a contribution. The Custom fields are associated by Financial Types,  I intend to show the current contact id of the person the contribution is being submitted for in a custom field. I tried doing this originally within the CustomDatabyType.php file.</p>
  
  <pre><code>class CRM_Custom_Form_CustomDataByType extends CRM_Core_Form {
  
    public function setDefaultValues() {
      $contact_id = CRM_Core_Form::getContactID();
      $defaults = [];
      $defaults['custom_224_-1'] = $contact_id;
      CRM_Core_BAO_CustomGroup::setDefaults($this-&gt;_groupTree, $defaults, FALSE, FALSE, $this-&gt;get('action'));
      return $defaults;
    }
  }
  </code></pre>
  
  <p>To Clarify the reason i want the contact ID for the user i am submitting the form for is because i want to display that contacts 'Employer' in a custom field. I have tried the following:</p>
  
  <pre><code>CRM_Utils_Request::retrieve('cid', 'Positive');
  $form-&gt;getContactID();
  $form-&gt;_contactID;
  </code></pre>
  
  <p>The issue i am having is that is the above lines of code work if i associate it to a field within the contribution form but if i associate it to a custom field it will show the contactID of the user submitting the form not the user of whom the form is being submitting for.</p>
  
  <p>I have also tried to do this via a custom buildForm hook:</p>
  
  <pre><code>function dfi_civicrm_buildForm($formName, &amp;$form) {
  
      if ($formName == 'CRM_Contribute_Form_Contribution' || $form == CRM_Core_Action::ADD) {
          $thisContactID = $form-&gt;_contactID;
          $defaults['custom_224_-1'] = $thisContactID;
         $form-&gt;setDefaults($defaults);
      }
  }
  </code></pre>
  
  <p>I hope i have been clear and i would really appreciate your help.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>An Idea i have had is to set the ContactID within a field in the 'Contribution section' use getVar to get the value of the field and then assign that value to the custom field.</p>
  
  <p>Is this possible?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-14T20:55:09.800Z
Comments.Text: Try instead with hook_civicrm_buildForm and use `$form->setDefaults()`
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-18T13:02:14.410Z
Comments.Text: @Demerit - Its not working unfortunately.
Comments.UserDisplayName: Moe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-20T08:15:53.843Z
Comments.Text: https://github.com/civicrm/civicrm-core/pull/15012#issuecomment-520766849 - Found this however it only works for Activity Custom Fields not Contribution.
Comments.UserDisplayName: Moe
Tags: <contacts>
Title: Unable to get Contact ID to display in Custom Field
ThreadText: # Unable to get Contact ID to display in Custom Field
  
  (Asked by: username_0 on 2019-11-14)
  
  Been stuck on this for the past few days I want to set a default value for a custom field in a contribution. The Custom fields are associated by Financial Types, I intend to show the current contact id of the person the contribution is being submitted for in a custom field. I tried doing this originally within the CustomDatabyType.php file.
  
  ```
  class CRM_Custom_Form_CustomDataByType extends CRM_Core_Form {
  
    public function setDefaultValues() {
      $contact_id = CRM_Core_Form::getContactID();
      $defaults = [];
      $defaults['custom_224_-1'] = $contact_id;
      CRM_Core_BAO_CustomGroup::setDefaults($this->_groupTree, $defaults, FALSE, FALSE, $this->get('action'));
      return $defaults;
    }
  }
  
  ```
  
  To Clarify the reason i want the contact ID for the user i am submitting the form for is because i want to display that contacts 'Employer' in a custom field. I have tried the following:
  
  ```
  CRM_Utils_Request::retrieve('cid', 'Positive');
  $form->getContactID();
  $form->_contactID;
  
  ```
  
  The issue i am having is that is the above lines of code work if i associate it to a field within the contribution form but if i associate it to a custom field it will show the contactID of the user submitting the form not the user of whom the form is being submitting for.
  
  I have also tried to do this via a custom buildForm hook:
  
  ```
  function dfi_civicrm_buildForm($formName, &$form) {
  
      if ($formName == 'CRM_Contribute_Form_Contribution' || $form == CRM_Core_Action::ADD) {
          $thisContactID = $form->_contactID;
          $defaults['custom_224_-1'] = $thisContactID;
         $form->setDefaults($defaults);
      }
  }
  
  ```
  
  I hope i have been clear and i would really appreciate your help.
  
  Thanks
  
  ---
  
  An Idea i have had is to set the ContactID within a field in the 'Contribution section' use getVar to get the value of the field and then assign that value to the custom field.
  
  Is this possible?
  
  **Tags:** contacts
  
  > **Comments:**
  > **username_1**: Try instead with hook_civicrm_buildForm and use `$form->setDefaults()`  
  > **username_0**: @username_1 - Its not working unfortunately.  
  > **username_0**: https://github.com/civicrm/civicrm-core/pull/15012#issuecomment-520766849 - Found this however it only works for Activity Custom Fields not Contribution.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-12-10. Score: 3 | Accepted answer
  
  I was able to figure this out using the following code:
  
  ```
          $getSessionArray = CRM_Core_Session::singleton()->get('CRM_Utils_Recent');
          $thisContactID = $getSessionArray[0]['id'];
  
  ```
  
  > **Comments:**
  > **username_1**: Great. Just a note for others that if you remove Contacts from the list at Administer - System Settings - Misc where it says "Recent Items Providers" then this might fail, but I'd guess that very few sites do that.  
  
  ---
  
  