CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Restricting ContactReference Autocomplete To Specific Contact Type
Pages: [1]

Author Topic: Restricting ContactReference Autocomplete To Specific Contact Type  (Read 857 times)

spork

  • I’m new here
  • *
  • Posts: 13
  • Karma: 1
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7
Restricting ContactReference Autocomplete To Specific Contact Type
December 06, 2010, 12:06:23 pm
Hi all,

I am working on setting up a site for collecting student teacher field experience. To model this I have created a "Student Teacher" contact type extending "Individual", and a "School" contact type extending "Organization". I have also added a "Field Experience" custom field set which includes begin date, end date, school, etc. Since a teaching student might have multiple field experiences I enabled the "allow multiple records" option too.

Anyway, the main item of concern is the "school" custom field in the "Field Experience" custom field set. I have it set as a ContactReference type so it can reference "School" type contacts. The issue is that whenever any inputs for this field appear, we want the autocomplete to be restricted to contacts of type "School" in order to make the autocomplete more useful and to prevent invalid submissions into the field (we also probably want to do some sort of validation too, to make sure anything submitted is only a School type).

Based on the autocomplete documentation I found in the CiviCRM book (http://en.flossmanuals.net/CiviCRM/DevelopAPI) I have tried experimenting with getting autocomplete to work by viewing the preview page for the custom field. Seeing that the preview page uses 'CRM\Custom\Page\Field.tpl' through an HTML comment in the page, I created custom overridden version of the template and simply threw the following code at the top (based off the book's example):

Code: [Select]
{if $session->get('userID') > 0}
<script type="text/javascript" src="{$config->resourceBase}js/rest.js"></script>
{literal}
<script>
jQuery(document).ready(function($){
  $('#custom_7_-1').crmAutocomplete({params:{contact_type:'School'}});
});
</script>
{/literal}
{/if}

where '#custom_7_-1' was the id of the input element for the custom field. I can see that the code I added was inserted, however, the autocomplete for the custom field simply stopped working, so I'm guessing I am doing this wrong. In particular, I am wondering what element should we selecting for the 'crmAutocomplete' method to act on? I figured an HTML input but I am not sure. Any insights?

Thanks,
James

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Restricting ContactReference Autocomplete To Specific Contact Type
December 06, 2010, 12:25:07 pm
I wrote a while ago a post about it, I must have been thinking about you ;)

http://civicrm.org/blogs/xavier/how-customise-contact-reference-custom-field

Check on Brian's comment, he describes another way of doing the same thing.

X+

-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

spork

  • I’m new here
  • *
  • Posts: 13
  • Karma: 1
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7
Re: Restricting ContactReference Autocomplete To Specific Contact Type
December 06, 2010, 03:37:08 pm
Sweet, thanks. I actually ended up using Brian's (lcdweb's) way as it's preferable for us to be able to package this in a module and it seems to be working well. Your way is good too however.  ;)

A nice feature for future versions of CiviCRM might be to have an extra drop down list option when creating a ContactReference custom field type that lets you select a contact sub type (and perhaps other filters like tags, groups, and what not).

James

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Restricting ContactReference Autocomplete To Specific Contact Type

This forum was archived on 2017-11-26.