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 (Moderator: Donald Lobo) »
  • Using the CiviCRM API in AJAX from a Drupal page
Pages: [1]

Author Topic: Using the CiviCRM API in AJAX from a Drupal page  (Read 520 times)

JohnFF

  • I post frequently
  • ***
  • Posts: 235
  • Karma: 6
  • CiviCRM version: 4.4.13
  • CMS version: Drupal 7.28
  • MySQL version: 5.5.31-1
  • PHP version: 5.3.27
Using the CiviCRM API in AJAX from a Drupal page
August 05, 2014, 12:10:35 pm
Hi gang,

How can we use the CiviCRM API in an AJAX call from a Drupal page?

We're trying to update the status of a relationship (active/inactive) when a button is clicked.

The doc I read says 'you might be able to, but it's not supported, tell us if you make it work'.

All help appreciated!
If you like empowering charities in a free and open way, then you're going to love Civi.

Email Amender: https://civicrm.org/extensions/email-amender
UK Phone Validator: https://civicrm.org/extensions/uk-phone-number-validator
http://civifirst.com
https://twitter.com/civifirst

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Using the CiviCRM API in AJAX from a Drupal page
August 05, 2014, 12:15:28 pm
There are 2 ways I know of to make this work:
1) Easier: Include the civicrm js on your page by doing CRM_Core_Resources::singleton()->addCoreResources();, then you will have access to CRM.api().
2) Lighter Weight: Just ping the api yourself by doing jQuery.ajax(). Use the api explorer to help you construct the correct url (or just notice what url is being called via firebug when you run CRM.api)
Let me know how it goes (bonus points if you update the docs).
Try asking your question on the new CiviCRM help site.

JohnFF

  • I post frequently
  • ***
  • Posts: 235
  • Karma: 6
  • CiviCRM version: 4.4.13
  • CMS version: Drupal 7.28
  • MySQL version: 5.5.31-1
  • PHP version: 5.3.27
Re: Using the CiviCRM API in AJAX from a Drupal page
August 06, 2014, 06:17:08 am
Thanks Coleman, that's great :D
If you like empowering charities in a free and open way, then you're going to love Civi.

Email Amender: https://civicrm.org/extensions/email-amender
UK Phone Validator: https://civicrm.org/extensions/uk-phone-number-validator
http://civifirst.com
https://twitter.com/civifirst

awasson

  • I post frequently
  • ***
  • Posts: 230
  • Karma: 7
  • Living in a world of Drupal / CiviCRM
    • My Company: Luna Design
  • CiviCRM version: Latest
  • CMS version: Drupal 6/7/8
  • MySQL version: 5.x
  • PHP version: 5.3.x
Re: Using the CiviCRM API in AJAX from a Drupal page
August 14, 2014, 05:43:19 pm
I'm a little late to the game but I had a similar need a few weeks ago. From a Drupal page, I needed to create a relationship between one CiviCRM contact and another. In order to do so, I needed to load the CiviCRM jQuery API and I needed the CiviCRM Contact ID's and the ID number of the relationship I wanted to apply.

I used the API explorer to figure out the correct jQuery to make it work (www.example.com/civicrm/api/explorer)

Here is my proof of concept:

Note: I was using a View to show the relationships in a widget (block) and I made reference of that in my code below.
Contact A ID = 2
Contact B ID = 25
Relationship ID = 11

Code: [Select]

# USE PHP TO LOAD CORE JAVASCRIPT
<?php
    civicrm_initialize
();
    
$manager = CRM_Core_Resources::singleton();
    
$manager->addCoreResources();
?>


<script>
// Javascript to create a relationship between Contact A & COntact B for Relationship Type = 11
CRM.api('Relationship', 'create', {'sequential': 1, 'contact_id_a': 2, 'contact_id_b': 25, 'relationship_type_id': 11},
  {success: function(data) {
      cj.each(data, function(key, value) {
        // Refresh the view here to show the updated information in the widget.
      });
    }
  }
);
</script>

 
My CiviCRM Extension Workshop: https://github.com/awasson

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Using the CiviCRM API in AJAX from a Drupal page

This forum was archived on 2017-11-26.