function _webform_civicrm_webform_frontend_form_alter in Webform CiviCRM Integration 7
Same name and namespace in other branches
- 6.2 webform_civicrm_forms.inc \_webform_civicrm_webform_frontend_form_alter()
- 6 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 - Implements hook_form_alter().
File
- ./
webform_civicrm_forms.inc, line 516
Code
function _webform_civicrm_webform_frontend_form_alter(&$form) {
civicrm_initialize();
require_once 'api/api.php';
$cid = $existing = 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 (array_key_exists('#submission', $form)) {
if (isset($form['#submission']->civicrm['contact_id'])) {
$cid = $form['#submission']->civicrm['contact_id'];
$existing = 1;
}
}
if (!$cid) {
// If user is logged in, look up the CID
global $user;
if ($user->uid) {
$result = civicrm_api('uf_match', 'get', array(
'uf_id' => $user->uid,
'check_permissions' => FALSE,
'version' => 3,
));
$result = array_pop($result['values']);
$cid = $result['contact_id'];
}
// If user is following a hashed link from CiviMail, validate it and set CID appropriately
if (!empty($_GET['cid']) && !empty($_GET['cs'])) {
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'] = check_markup($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
$params = array(
'contact_id' => $cid,
'check_permissions' => FALSE,
'version' => 3,
'check_permission' => FALSE,
);
$contact = civicrm_api('contact', 'get', $params);
$contact = array_pop($contact['values']);
$fields = webform_civicrm_get_fields();
$custom = FALSE;
foreach ($fields as $id => $f) {
if (strpos($id, 'custom') || strpos($id, 'preferred_communication_method')) {
$custom = TRUE;
$params[str_replace('civicrm_', 'return.', $id)] = 1;
}
}
if ($custom) {
$custom = civicrm_api('contact', 'get', $params);
$contact = array_merge($contact, array_pop($custom['values']));
}
if ($settings['prefix_known']) {
$form['#prefix'] = check_markup(webform_civicrm_replace_tokens($settings['prefix_known'], $contact));
}
// Do not alter form default values if this is an edit op
if ($existing) {
return;
}
// Set default values for group field
if (isset($enabled['civicrm_groups'])) {
$groups = civicrm_api('group_contact', 'get', array(
'check_permissions' => FALSE,
'version' => 3,
'contact_id' => $cid,
));
$contact['groups'] = array();
foreach ($groups['values'] as $group) {
$contact['groups'][] = $group['group_id'];
}
}
// Recurse through FAPI array and set default values for civicrm elements
webform_civicrm_fill_values($form['submitted'], $contact);
if ($settings['message']) {
webform_civicrm_set_message($settings['message'], $contact);
}
}