function wf_crm_fill_form in Webform CiviCRM Integration 7.3
Recursively walk through form array and set properties of CiviCRM fields Called by _wf_crm_frontend_form_alter() when webform is being viewed
Parameters
$elements: FAPI form array
$node: Node object
$data: CiviCRM data to autofill form (optional)
$submitted: Existing submission (optional)
1 call to wf_crm_fill_form()
- _wf_crm_frontend_form_alter in ./
webform_civicrm_forms.inc - Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.
File
- ./
webform_civicrm_forms.inc, line 437
Code
function wf_crm_fill_form(&$elements, $node, $data = array(), $submitted = array()) {
$sp = CRM_Core_DAO::VALUE_SEPARATOR;
$fields = wf_crm_get_fields();
foreach ($elements as $eid => &$element) {
if ($eid[0] == '#' || !is_array($element)) {
continue;
}
// Recurse through nested elements
wf_crm_fill_form($element, $node, $data, $submitted);
if (empty($element['#type']) || $element['#type'] == 'fieldset') {
continue;
}
if (!empty($element['#webform_component']) && ($pieces = wf_crm_explode_key($eid))) {
list(, $c, $ent, $n, $table, $name) = $pieces;
// Separate out time fields
if (substr($name, -8) === 'timepart') {
$name = str_replace('_timepart', '', $name);
}
if ($field = wf_crm_aval($fields, $table . '_' . $name)) {
$component = $element['#webform_component'];
$element['#attributes']['class'][] = 'civicrm-enabled';
if (!empty($field['data_type'])) {
$dt = $element['#civicrm_data_type'] = $field['data_type'];
// Add CiviCRM JS to link fields
if ($dt == 'Link' && substr($element['#type'], 0, 4) == 'text') {
$element['#attributes']['onblur'] = "if (this.value == 'http://') {this.value = '';}";
$element['#attributes']['onfocus'] = "if (this.value == '') {this.value = 'http://';}";
}
}
elseif (!empty($component['extra']['civicrm_live_options']) && isset($element['#options'])) {
$params = array(
'extra' => wf_crm_aval($field, 'extra', array()),
) + $component;
$new = wf_crm_field_options($params, 'live_options', $node->webform_civicrm['data']);
$old = $element['#options'];
$resave = FALSE;
// If an item doesn't exist, we add it. If it's changed, we update it.
// But we don't subtract items that have been removed in civi - this prevents
// breaking the display of old submissions.
foreach ($new as $k => $v) {
if (!isset($old[$k]) || $old[$k] != $v) {
$old[$k] = $v;
$resave = TRUE;
}
}
if ($resave) {
$component['extra']['items'] = wf_crm_array2str($old);
webform_component_update($component);
}
$element['#options'] = $new;
}
// If the user has already entered a value for this field, don't change it
if (isset($data[$ent][$c][$table][$n][$name]) && !(isset($component['cid']) && isset($submitted[$component['cid']]))) {
$val = $data[$ent][$c][$table][$n][$name];
if (($element['#type'] == 'checkboxes' || !empty($element['#multiple'])) && !is_array($val)) {
$val = explode($sp, trim($val, $sp));
}
if ($element['#type'] != 'checkboxes' && $element['#type'] != 'date' && empty($element['#multiple']) && is_array($val)) {
// If there's more than one value for a non-multi field, pick the most appropriate
if (!empty($element['#options'])) {
foreach ($element['#options'] as $k => $v) {
if (in_array($k, $val)) {
$val = $k;
break;
}
}
}
else {
$val = array_pop($val);
}
}
// Set data for contact image
if ($name == 'image_URL') {
if ($val && in_array($element['#type'], array(
'file',
'managed_file',
))) {
$js = "jQuery(function() {wfCivi.contactImage('{$eid}', '{$val}')});";
$element['#attached']['js'][$js] = array(
'type' => 'inline',
);
}
}
elseif ($element['#type'] == 'value') {
$element['#value'] = $val;
}
else {
$element['#default_value'] = $val;
}
}
if ($name == 'existing') {
wf_crm_fill_contact_value($node, $component, $element);
}
}
}
}
}