$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.

  • 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. – Demerit Nov 28 '19 at 3:09
  • Um, well yes. What I'm getting at is there does not appear to be anything which actually includes the cid. – Simon Walden Nov 28 '19 at 8:13

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.)

| improve this answer | |
  • 1
    edited to give more info, but In this case the cust data is in an Activity for a civiCase – Simon Walden Nov 27 '19 at 12:12
  • 2
    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']. – Demerit Nov 28 '19 at 19:54
  • 1
    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. – Simon Walden Nov 29 '19 at 8:32

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.