/** * Pre-fill contact name for a custom field of type ContactReference * * Todo: Migrate contact reference fields to use EntityRef * * @param HTML_QuickForm_element $field */ public static function preprocessContactReference($field) { $val = $field->getValue(); if ($val && is_numeric($val)) { $list = array_keys(CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_reference_options'), '1'); $return = array_unique(array_merge(array('sort_name'), $list)); $contact = civicrm_api('contact', 'getsingle', array('id' => $val, 'return' => $return, 'version' => 3)); if (!empty($contact['id'])) { $view = array(); foreach ($return as $fld) { if (!empty($contact[$fld])) { $view[] = $contact[$fld]; } } $field->setAttribute('data-entity-value', json_encode(array('id' => $contact['id'], 'text' => implode(' :: ', $view)))); } } }