Is it possible to extract the target contact ID of a form during hook_civicrm_buildForm?


We are trying to manipulate a wide range of fields across the site, ones that might be present in event registration forms, contact profile forms, etc.

We want to disable editing of the field if there is a value in it, and the contact has a certain value on their record.

The workflow we have in mind is...

  • Is 'Field A' on the form?
  • If yes, does 'Field A' have a value already?
  • If yes, is there a 'target contact' for the form (ie: a logged in user is registering for an event, or updating their profile)
  • If yes, fire an API call to get the value of 'Field X' from the record of the target contact.
  • If 'Field X' is 'Yes' then disable editing of 'Field A' on the form.

We understand that not all forms will have a target contact ID, and we would only want to go through this logic if there was one. So it wouldn't do any of this if an anonymous user was signing up to an event; but would if a logged-in user was.

The logic itself isn't the problem, but we can't seem to find the target contact ID stored anywhere at this point.

Thanks in advance!


There is no target contact ID stored in the $form, however...

As ErikH mentioned, most of the time the logged-in user fills in the form themselves. In that case, you could retrieve the contact ID like this:

$session = CRM_Core_Session::singleton();
$contactId = $session->get('userID');

If another user or admin is filling in the form for another contact, the URL should have something like cid=xxx specified. It's true for participant registration and profile editing.

There is still the case of a new contact, either with cid=0, either if not logged in (session does not contains userID).


Not sure about them, but you can at least always retrieve the logged in user? Like so:

$session = CRM_Core_Session::singleton();
$userId = $session->get('userID');
  • Thanks Erik, unfortunately this doesn't help in the case where an administrator is updating a user's profile.
    – John
    Mar 31 '15 at 14:59
  • True enough :-). Have you checked the entryUrl to see if there is anything valuable in there? Mar 31 '15 at 15:03
  • Which form are you catching in your buildForm hook? Is it not easier to use the civicrm_post hook on the Participant entity? Just thinking out loud here..... Mar 31 '15 at 15:09
  • So we're particularly interested in the event registration form, and the profile update form. There is nothing in the entryURL for either. I've done some searching in the $form for 'my' contact ID and nothing is coming up, so I guess this isn't possible - at least not in a 'generic' way.
    – John
    Mar 31 '15 at 15:11
  • What are you trying to achieve? Could you solve it with the civicrm_post or civicrm_pre hook? If for example I get a Participant entity in the post hook, I can then check if field X exists for the participant? Mar 31 '15 at 15:18

It really depends on the entity you are processing I guess....if you are working on the activity form, you can get the activity id and with that id retrieve the target it from the API? Or from the table civicrm_activity_contact directly using CRM_Core_DAO::executeQuery?

  • I'm particularly focusing on the profile update form and the event registration form - neither of these seem to have the associated contact ID in the $form when someone is logged in?
    – John
    Mar 31 '15 at 14:54
  • Not sure about them, but you can at least always retrieve the logged in user? Like so: Mar 31 '15 at 14:56

Just in case, alternative solution via hook in Drupal module:

function MYMODULENAME_civicrm_buildForm($formName, &$form) {
# Form we work with, print_r($formName) for name;
  if ($formName == 'CRM_Profile_Form_Edit') {
     $curr_user_civiID = $_SESSION['CiviCRM']['userID']; 
# Check if it matches current user's civicrm contactID via print_r($curr_user_civiID);

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.