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) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting more data from hook_civicrm_contactListQuery
Pages: [1]

Author Topic: Getting more data from hook_civicrm_contactListQuery  (Read 1465 times)

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Getting more data from hook_civicrm_contactListQuery
October 16, 2011, 04:32:20 am
I have a custom query set up in my module using the contactListQuery hook, it works just fine (sql query matching and returning all fields I'm interested in, but I am having a bit of trouble figuring out how to actually get more than just the two "contact id and sort_name" fields returned via the request. Is this possible?

I am creating a mod to the event participant add page. It has a contact field with autocomplete. Once a contact of type individual is selected, I am trying to get the contact's id, then its employee of id, the employer's membership id, membership type id, and membership type name.

If not, is there any other way to fetch this data via ajax so that I can populate the fields?

Any help would be apprecaited, thank you.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Getting more data from hook_civicrm_contactListQuery
October 16, 2011, 07:54:44 am
Hi,

I would use the api.contact.get (set the return=sort_name,organization_name...) Not sure if you can get directly the membership, use the api explorer and check out what the membership api returns. You might need a second call

http://en.flossmanuals.net/civicrm-developer-guide/api/ and the blog post if you want to learn more about it.

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

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Getting more data from hook_civicrm_contactListQuery
October 16, 2011, 11:15:13 am
Ok, that looks like it might work:

I'm looking at my /civicrm/api/explorer#/drupal7/civicrm/ajax/rest?json=1&debug=1&version=3&entity=Membership&action=get

which returns:

{
   "is_error":0,
   "version":3,
   "count":15,
   "values":{
      "1":{
         "id":"1",
         "contact_id":"49734",
         "membership_type_id":"2",
         "join_date":"2011-10-03",
         "start_date":"2011-10-03",
         "end_date":"2012-11-02",
         "status_id":"1",
         "is_test":"0",
         "is_pay_later":"0",
         "membership_name":"Corporate",
         "relationship_name":"Employee of",
         "custom_6":"2",
         "custom_6_1":"2",
         "custom_8":"5",
         "custom_8_-1":"5"
      },...all the way to value 15.


Is there a way to modify the ajax call with parameters in order to only limit the return to only those memberships with a matching contact_id? I'm only looking to for values on a specific membership at a time.

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Getting more data from hook_civicrm_contactListQuery
October 16, 2011, 12:30:25 pm
Ok, I've gone a step further. I now have a button trying to pass the API call.

My problem is that the URL its going after is not correct.

My install is localhost/drupal7/civicrm.

http://localhost/civicrm/ajax/rest?version=3&contact_id=36049&entity=Membership&action=get&json=1

Where does the source path come from?

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Getting more data from hook_civicrm_contactListQuery
October 16, 2011, 12:58:08 pm
Are you using cj.crmAPI()? the 4th param allows you to set the base url.
eg.
{'ajaxURL': '/drupal7/civicrm/ajax/rest'} should work

as for the contact_id, should work if you simply put it in the second param {'contact_id':42}
X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

DerekL

  • I post frequently
  • ***
  • Posts: 132
  • Karma: 1
  • CiviCRM version: 4.5.5
  • CMS version: Drupal 7.34
  • MySQL version: 5.1.54
  • PHP version: 5.2.17
Re: Getting more data from hook_civicrm_contactListQuery
October 16, 2011, 01:54:06 pm
Beautiful! Thanks Xavier. I was trying to pass ajaxURL via var options, as per the comments on rest.js, but that was leading me nowhere fast.

This is what the comments say on rest.js :
Code: [Select]
If you do not use clean urls on drupal, you have to define a variable to set the url of the server to be used for the rest
<script type="text/javascript">
var options {ajaxURL:"{$config->userFrameworkResourceURL}";
</script>

Here is my current, working test code, set to trigger on a button click, contact_id hardcoded for now. Its ugly as sin, and hard to work with... I'm wondering if there is a more elegant way to parse the result.

Code: [Select]
cj( function( ) {

cj('#getmembership').click(function() {
//ready for function to call on click event


  cj().crmAPI ('Membership','get',{'version' :'3', 'contact_id' : '36049'}
  ,{ 'ajaxURL': '/drupal7/civicrm/ajax/rest',
  success:function (data){
 
      cj.each(data, function(key, value) {
      if (key == 'values' ) {
      cj.each(value, function(key1,value1) {
       
      cj.each(value1, function(key2,value2) {
       
      alert(key2 + ': ' + value2);   
      });
      });
       
      }
      });
      }
//end function called on click event  
});
});
});




Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Getting more data from hook_civicrm_contactListQuery

This forum was archived on 2017-11-26.