Answers.Body: <p>Can you explain a little more what you are after in what circumstances? </p>
  
  <p>Generally speaking: when in a form Custom Data I would expect to see something like entityId or _entityID. Depending on the defintion of the custom group this would be the contact_id (or membership or activity etc.)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-27T12:12:20.703Z
Answers.Text: edited to give more info, but In this case the cust data is in an Activity for a civiCase
Answers.UserDisplayName: Simon Walden
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-28T19:54:36.707Z
Answers.Text: As Erik notes there is `$form->_entityId`, and then assuming you mean the "reported by"/"author" contact of the existing activity being edited, you can do `$result = civicrm_api3('Activity', 'get', ['return' => ["source_contact_id"], 'id' => $form->_entityId,]);` and then the contact id is in `$result['values']['source_contact_id']`.
Answers.UserDisplayName: Demerit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-29T08:32:21.283Z
Answers.Text: OK, so the contact ID is not present in the form as I suspected.   Frustrating that the other forms of getting the contactID aren't maintained, but maybe there is a reason.
  
  For other readers, just to clarify:  ErikH's comment only gets you the current entityID - so in other words an activityID if the current entity is an activity etc.   It doesn't get you the contactID unless you are in a contact.
Answers.UserDisplayName: Simon Walden
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-27T11:06:10.563Z
Answers.LastActivityDate: 2019-11-27T11:06:10.563Z
Answers.UserDisplayName: ErikH - CiviCooP
Body: <pre><code>$contactId = CRM_Utils_Request::retrieve('cid', 'Positive');
  $contactId = (int) $_GET['cid'];
  $contactId = $form-&gt;getVar('_contactID');
  </code></pre>
  
  <p>Are all possible solutions at different places, but none seem to work when in a Form_CustomData.</p>
  
  <p>In this case the cust data is in an Activity for a civiCase</p>
  
  <p>For the avoidance of doubt, this is to get the ID for the contact (not the user)</p>
  
  <p><em>Edit: additional info.</em></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-28T03:09:31.447Z
Comments.Text: Use any debugging technique to examine the $form variable at the point in the code where you want to use it, and see what's available. e.g. CRM_Core_Error::debug_log_message(print_r($form, true)); to write to ConfigAndLog.
Comments.UserDisplayName: Demerit
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-11-28T08:13:38.337Z
Comments.Text: Um, well yes.   What I'm getting at is there does not appear to be anything which actually includes the cid.
Comments.UserDisplayName: Simon Walden
Tags: <custom-fields>
Title: How do you get contact_id in a custom form?
ThreadText: # How do you get contact_id in a custom form?
  
  (Asked by: username_0 on 2019-11-27)
  
  ```
  $contactId = CRM_Utils_Request::retrieve('cid', 'Positive');
  $contactId = (int) $_GET['cid'];
  $contactId = $form->getVar('_contactID');
  
  ```
  
  Are all possible solutions at different places, but none seem to work when in a Form\_CustomData.
  
  In this case the cust data is in an Activity for a civiCase
  
  For the avoidance of doubt, this is to get the ID for the contact (not the user)
  
  *Edit: additional info.*
  
  **Tags:** custom-fields
  
  > **Comments:**
  > **username_1**: Use any debugging technique to examine the $form variable at the point in the code where you want to use it, and see what's available. e.g. CRM_Core_Error::debug_log_message(print_r($form, true)); to write to ConfigAndLog.  
  > **username_0**: Um, well yes.   What I'm getting at is there does not appear to be anything which actually includes the cid.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-11-27. Score: 1
  
  Can you explain a little more what you are after in what circumstances?
  
  Generally speaking: when in a form Custom Data I would expect to see something like entityId or \_entityID. Depending on the defintion of the custom group this would be the contact\_id (or membership or activity etc.)
  
  > **Comments:**
  > **username_0**: edited to give more info, but In this case the cust data is in an Activity for a civiCase  
  > **username_1**: As Erik notes there is `$form->_entityId`, and then assuming you mean the "reported by"/"author" contact of the existing activity being edited, you can do `$result = civicrm_api3('Activity', 'get', ['return' => ["source_contact_id"], 'id' => $form->_entityId,]);` and then the contact id is in `$result['values']['source_contact_id']`.  
  > **username_0**: OK, so the contact ID is not present in the form as I suspected.   Frustrating that the other forms of getting the contactID aren't maintained, but maybe there is a reason.  For other readers, just to clarify:  username_2's comment only gets you the current entityID - so in other words an activityID if the current entity is an activity etc.   It doesn't get you the contactID unless you are in a contact.  
  
  ---
  
  