I'm writing an extension to customize the create new activity page. I need to add the 'With Contact' address and phone number to the page. When editing the /civicrm/CRM/Activity/Form/Activity.php how do you get the address to propagate when the contact is selected? Here is what I'm trying to edit:

   'target_contact_id' => array(
   'type' => 'entityRef',
   'label' => ts('With Contact'),
   'attributes' => array('multiple' => TRUE, 'create' => TRUE),
   'target_contact_adress' => array(
   'type' => 'entityRef', //I'm guessing I need to change this line to call the address?
   'label' => ts('With Contact Address'),
   'attributes' => array('multiple' => TRUE, 'create' => TRUE),

Edit: To better explain what I'm trying to do. I'm creating a custom extension for an organization that uses the Activity assignment notification to email their volunteers that don't have access to the CiviCRM database contact information (Phone Numbers and Addresses) about their members. In order to do this I need to add two fields that pull this information when a contact is selected.

  • I assume your code is part of a buildForm hook?
    – Coleman
    Commented Jun 30, 2015 at 20:10
  • I think I could answer your question if I knew exactly what you were asking. You want to create a new form field to select... what exactly? And why?
    – Coleman
    Commented Jun 30, 2015 at 20:11
  • Correct, It's part of a buildFrom hook. The form field should select the address/phone number of the selected entity (Individual). Why, because this organization is using the Activity email notification to let their volunteers know the number and address of their "members". Thank you for any help.
    – Sean
    Commented Jun 30, 2015 at 20:47
  • Still trying to understand... so once they've selcted the "With Contact" (note this can be multivalued!) you want them to be able to select one of potentially multiple addresses/phones for that contact? And why? What will the form submission do with that information?
    – Coleman
    Commented Jun 30, 2015 at 21:09
  • Sorry for not being clear. This is my first extension for CiviCRM. I'm using this project to learn. Let me give an example scenario. This organization assists elders in the community by providing services, one of these services is to give rides to various places (let's say a pharmacy). So, they need a volunteer (the assigned to contact) to give a ride to the elder (the with contact). When an activity is assigned the volunteer gets a notification by email (Volunteers do not have access to CiviCRM). Now, there is already a location field (the pharmacy). Countinued...
    – Sean
    Commented Jun 30, 2015 at 21:32

1 Answer 1


I'd suggest you create 2 custom fields for this in the activity (pickup location & contact phone number). You can then use a bit of javascript to insert the contact's email & phone into those fields and it will come through in the activity email. As an added bonus, this approach gives the flexibility of allowing the user to override those defaults if the person needs to be picked up elsewhere.

To do this, your buildform hook needs to do 2 things:

  1. Request the extra address/phone info from the target_contact_id field. You can do:

    $form->_fields['target_contact_id']['attributes']['api']['extra'] = array(
  2. Add your javascript to the form using CRM_Core_Resources (hint: create a js file in your extension, and use CRM_Core_Resources::singleton()->addScriptFile('myExtension', 'js/myScript.js'))

Your javascript can then use some basic jQuery like:

CRM.$(function($) {
  $('#target_contact_id').change(function() {
    if ($(this).val()) {
      // This contains the extra fields you requested
      var data = $(this).select2('data')[0].extra;
      // For example, populate a custom field with the phone

You can read more about javascript in CiviCRM here.

  • Awesome! Worked like a charm. You're crushing it on this StackExchange. Thanks again for all your hard work on here.
    – Sean
    Commented Jul 1, 2015 at 1:55
  • Glad to help. Good luck with your project :)
    – Coleman
    Commented Jul 1, 2015 at 2:01
  • @Sean - curious to see how your code turned out - want to paste a link to it (on github, gist, etc.)
    – Coleman
    Commented Jul 2, 2015 at 0:20
  • Absolutely, I'm vacationing in the mountains and have limited access to the internet. I will send it over to you as soon as I get back.
    – Sean
    Commented Jul 4, 2015 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.