function _webform_civicrm_webform_frontend_form_alter in Webform CiviCRM Integration 6
Same name and namespace in other branches
- 6.2 webform_civicrm_forms.inc \_webform_civicrm_webform_frontend_form_alter()
- 7 webform_civicrm_forms.inc \_webform_civicrm_webform_frontend_form_alter()
- 7.2 webform_civicrm_forms.inc \_webform_civicrm_webform_frontend_form_alter()
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display message. Block unknown users. Set webform default values.
1 call to _webform_civicrm_webform_frontend_form_alter()
- webform_civicrm_form_alter in ./
webform_civicrm.module - Implementation of hook_form_alter().
File
- ./
webform_civicrm_forms.inc, line 519
Code
function _webform_civicrm_webform_frontend_form_alter(&$form) {
civicrm_initialize();
$cid = 0;
$node = $form['#node'];
$settings = $node->webform_civicrm;
$enabled = webform_civicrm_enabled_fields($node);
// If this is an edit op, use the original CID to avoid confusion
if (!empty($form['#submission'])) {
$submission = $form['#submission'];
$cid = $submission->civicrm['contact_id'];
}
else {
// If user is logged in, look up the CID
global $user;
if ($user->uid) {
require_once 'CRM/Core/BAO/UFMatch.php';
$cid = CRM_Core_BAO_UFMatch::getContactId($user->uid);
}
elseif (!empty($_GET['cs']) && !empty($_GET['cid'])) {
require_once 'CRM/Contact/BAO/Contact/Utils.php';
if (CRM_Contact_BAO_Contact_Utils::validChecksum($_GET['cid'], $_GET['cs'])) {
$_SESSION['webform_civicrm_cid'] = $cid = $_GET['cid'];
$_SESSION['webform_civicrm_cs'] = $_GET['cs'];
}
}
}
// Form alterations for unknown contacts
if (!$cid) {
$form['#prefix'] = filter_filter('process', 1, NULL, $settings['prefix_unknown']);
if ($settings['block_unknown_users']) {
$form['#access'] = FALSE;
drupal_set_message(t('Sorry, you do not have permission to access this form.'), 'warning');
}
return;
}
// Form alterations for known contacts
require_once 'api/v2/Contact.php';
$params = array(
'contact_id' => $cid,
);
$contact = civicrm_contact_get($params);
$fields = webform_civicrm_get_fields();
foreach ($fields as $id => $f) {
if (strpos($id, 'custom') || strpos($id, 'preferred_communication_method')) {
$params[str_replace('civicrm_', 'return.', $id)] = 1;
}
}
$custom = civicrm_contact_get($params);
$contact = array_merge($contact[$cid], $custom[$cid]);
$form['#prefix'] = filter_filter('process', 1, NULL, webform_civicrm_replace_tokens($settings['prefix_known'], $contact));
// Do not alter form default values if this is an edit op
if (!empty($submission)) {
return;
}
// Some translation of array key strings is necessary due to irregulatities in CiviCRM API v2
$contact_info = array();
foreach ($enabled as $id => $val) {
$new_id = str_replace(array(
'civicrm_',
'prefix',
'suffix',
), array(
'',
'individual_prefix',
'individual_suffix',
), $id);
if (isset($contact[$new_id])) {
$contact_info[$id] = $contact[$new_id];
}
}
// Set default values for group field
if (!empty($enabled['civicrm_groups'])) {
require_once 'api/v2/GroupContact.php';
$params = array(
'contact_id' => $cid,
);
$groups = civicrm_group_contact_get($params);
$contact_info['civicrm_groups'] = array();
foreach ($groups as $group) {
$contact_info['civicrm_groups'][] = $group['group_id'];
}
}
// Recurse through FAPI array and set default values for civicrm elements
webform_civicrm_fill_values($form['submitted'], $contact_info);
if ($settings['message']) {
webform_civicrm_set_message($settings['message'], $contact);
}
}