Integrating Drupal forms with BigContacts CRM

Submitted by Luke Dekker on 08/10/2016 - 01:33:pm

Integrating your Drupal site with an external service such as BigContacts might seem like a big task, but you can get a base integration completed in an afternoon if you have the right tools available. Keep in mind that not all integrations are the same. BigContacts does have an API, where you could do a full blown integration but for now we’ll just make it so that whenever someone submits a webform on our Drupal site, it automatically gets pushed to BigContacts.

First things first, we need a function that actually pushes our submitted results to BigContacts.

  1. /**
  2. * Submit handler for submitting webforms to Big Contacts
  3. */
  4. function mymodule_submit_webform_values_to_big_contacts($form, &$form_state) {
  5.   // Select just the information we want.
  6.   $submitted = $form_state['values']['submitted_tree'];
  7.  
  8.   // Updated on 12/13/13 based on BC upgrade
  9.  
  10.   // This makes the assumption that the fields are named the same in both
  11.   // Drupal and BigContacts
  12.   foreach ($submitted as $key => $value) {
  13.       $data[] = urlencode($key) . '=' . urlencode($value);
  14.   }
  15.   $data_string = implode('&', $data);
  16.   $request_headers = array('Content-Type' => 'application/x-www-form-urlencoded');
  17.   $request_method = 'POST';
  18.   $request_retry = 3;
  19.   $result = drupal_http_request($url, $request_headers, $request_method, $data_string, $request_retry);

That’s the biggest piece of the puzzle. Now we just need to link this submit handler to our webforms. We do this by hooking into the webform submission process.

  1. /**
  2. * Implements hook_form_alter().
  3. *
  4. * If we were only submitting one form to BigContacts, then we could use
  5. * hook_form_FORM_ID_alter(), but since in our case, we had multiple, it’s
  6. * cleaner to do it all from hook_form_alter().
  7. */
  8. function mymodule_form_alter(&$form, &$form_state, $form_id) {
  9.   // We store our form ids in variable so that we can update which forms integrate with BC
  10.   // without pushing any code changes. Depending on how in depth the deploy process is,
  11.   // this may allow you to quickly make a temporary change until you have a chance to
  12.   // deploy a more permanent code-based solution.
  13.   $bc_forms = variable_get(‘mymodule_bigcontact_forms’, ‘myform_form_id’)
  14.  
  15.   // Here we add our submit handler to the webform to make sure that the form results
  16.   // get pushed to BigContacts.
  17.   if (in_array($form_id, $bc_forms)) {
  18.       $form['#submit'][] = 'mymodule_submit_webform_values_to_big_contacts';
  19.   }
  20. }

And you’re done! All that is left is to clear caches to make sure that Drupal is aware of your changes and ready to react to them. This type of integration is possible with almost any 3rd party service and is just the tip of the iceberg of what is possible.