I would like to prepopulate a set of custom fields (on an activity) with data about the contact it pertains to. (The data I want to pull in is in a set of custom fields on the contact).

My idea was to use hook_civicrm_buildForm and look up the data by contact id, retrieve it from the API and place it into the form. But the $form array for the custom fields form does not contain the contact id.

Then I thought of using tokens to set default values in the custom fields (either for them all, or even just to set one to the contact id in order to solve the above issue) but I tried {contact.contact_id} and it came out just like that as a literal. Can tokens be used in custom fields on activities? If not, can you suggest another way of getting my hands on the contact id during the form building process?

  • There is probably an easier way to get the ID, but how about just pulling it from the URL since it is listed there?
    – Sleewok
    Dec 2, 2016 at 20:55

1 Answer 1


I ended up going for a very hacky solution.

The buildForm hook runs twice when you are adding/editing an activity with custom fields. Once for the activity and once for the custom fields. So, during the activity run I am taking the contact ID from the form array and putting it into $_SESSION. Then on the custom fields run I retrieve it and use it to get the values from the API with which to populate them.

I had tried getting the contact ID from the URL as Sleewok suggested, but it is not in there!

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.