Start by cloning the latest version of the api helper from https://github.com/jacobischwartz/emfl-api_php-lib to sites/all/libraries/emfl_api.
After that you'll create a custom module in sites/all/modules.
Start by cloning the latest version of the api helper from https://github.com/jacobischwartz/emfl-api_php-lib to sites/all/libraries/emfl_api.
After that you'll create a custom module in sites/all/modules.
<?php | |
/** | |
* Definitions | |
*/ | |
define('EMFLUENCE_PLATFORM_API_KEY', 'ff342fde-a672-9fbb-d5aa-0f87487f1682'); | |
define('EMFLUENCE_PLATFORM_ALL_USERS_LIST_ID', 0000); | |
define('EMFLUENCE_PLATFORM_SUBSCRIBERS_LIST_ID', 000000); | |
// The constants below are not the only place this number is used | |
// Be sure to update hook_{#} function as well | |
define('CUSTOM_WEBFORM_NID', 1); | |
/** | |
* Includes | |
*/ | |
require_once('webforms/webforms.forms.inc'); | |
/** | |
* Implementation of hook_{form_id}_alter | |
@see CUSTOM_WEBFORM_NID | |
* @param array $form | |
* @param array $form_state | |
*/ | |
function _custom_form_webform_client_form_1_alter(&$form, &$form_state){ | |
module_load_include('inc', 'custom', 'webform/webform.forms'); | |
_custom_form_webform_client_form_1_alter($form, $form_state); | |
} |
/** | |
* Implementation of hook_form_alter | |
*/ | |
function _custom_form_webform_client_form_1_alter(&$form, &$form_state) { | |
$form['#validate'][] = '_custom_form_webform_client_form_1_alter_validate'; | |
$form['#submit'][] = '_custom_form_webform_client_form_1_alter_submit'; | |
} | |
/** | |
* Implementation of hook_form_validate | |
*/ | |
function _custom_form_webform_client_form_1_alter_validate(&$form, &$form_state) { | |
$mailme_key = 'subscribe_to_newsletter'; | |
$vals = &$form_state['values']['submitted']; | |
$mailme = ($vals[$mailme_key][$mailme_key] === $mailme_key); | |
if ($mailme) { | |
$now_required = array( | |
'country', | |
'address_1', | |
'city', | |
'state', | |
'postal_code' | |
); | |
foreach ($now_required as $k) { | |
if (empty($vals['mailing_address'][$k])) { | |
form_set_error("submitted][mailing_address][$k", t('This field is required to be mailed a newslsetter.')); | |
} | |
} | |
} | |
} | |
/** | |
* Implementation of hook_form_submit | |
*/ | |
function _custom_form_webform_client_form_1_alter_submit(&$form, &$form_state) { | |
// Get form value function | |
$gfv = function($key, &$fsv) { | |
return !empty($fsv[$key]) ? $fsv[$key] : ''; | |
}; | |
// Form submitted values | |
$fsv = &$form_state['values']['submitted_tree']; | |
$data = array(); | |
$data['groupIDs'] = array(EMFLUENCE_PLATFORM_SUBSCRIBERS_LIST_ID); | |
$data['originalSource'] = 'example.com/connect'; | |
$data['action'] = 'add'; | |
$data['firstName'] = $gfv('first_name', $fsv); | |
$data['lastName'] = $gfv('last_name', $fsv); | |
$data['email'] = $gfv('email_address', $fsv); | |
$mailme_key = 'subscribe_to_newsletter'; | |
$mailme = false; | |
if (isset($fsv[$mailme_key])) { | |
if (isset($fsv[$mailme_key][0])) { | |
$mailme = ($fsv[$mailme_key][0] === $mailme_key); | |
} | |
} | |
if ($mailme) { | |
$data['groupIDs'][] = EMFLUENCE_PLATFORM_SUBSCRIBERS_LIST_ID; | |
$map = array( | |
'country' => 'country', | |
'address_1' => 'address1', | |
'address_2' => 'address2', | |
'city' => 'city', | |
'state' => 'state', | |
'postal_code' => 'zip', | |
); | |
foreach ($map as $f => $d) { | |
$data[$d] = $fsv['mailing_address'][$f]; | |
} | |
} | |
require_once( 'sites/all/libraries/emfl_api/api.class.inc' ); | |
$platform_api = new Emfl_Platform_API( EMFLUENCE_PLATFORM_API_KEY ); | |
$platform_api->contacts_save($data); | |
} |