You are here

function webform_civicrm_fill_values in Webform CiviCRM Integration 7.2

Same name and namespace in other branches
  1. 6.2 webform_civicrm_forms.inc \webform_civicrm_fill_values()
  2. 6 webform_civicrm_forms.inc \webform_civicrm_fill_values()
  3. 7 webform_civicrm_forms.inc \webform_civicrm_fill_values()

Recursively walk through form array and set default values for fields based on CiviCRM entity data Called by _webform_civicrm_webform_frontend_form_alter() when webform is being viewed @Param $elements: FAPI array @Param $data: Array of CiviCRM data

1 call to webform_civicrm_fill_values()
_webform_civicrm_webform_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 392

Code

function webform_civicrm_fill_values(&$elements, $data, $fields, $submitted) {
  $sp = CRM_Core_DAO::VALUE_SEPARATOR;
  foreach ($elements as $eid => &$element) {
    if ($eid[0] == '#' || !is_array($element)) {
      continue;
    }

    // Recurse through nested elements
    webform_civicrm_fill_values($element, $data, $fields, $submitted);
    if (empty($element['#type']) || $element['#type'] == 'fieldset' || $element['#type'] == 'file') {
      continue;
    }
    if ($pieces = webform_civicrm_explode_key($eid)) {
      list($lobo, $c, $ent, $n, $table, $name) = $pieces;
      if ($field = webform_civicrm_aval($fields, $table . '_' . $name)) {
        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://';}";
          }
        }

        // If the user has already entered a value for this field, don't change it
        if (isset($data[$ent][$c][$table][$n][$name]) && !(isset($element['#webform_component']['cid']) && isset($submitted[$element['#webform_component']['cid']]))) {
          $val = $data[$ent][$c][$table][$n][$name];
          if ($element['#type'] == 'date') {
            $date = strtotime($val);
            $val = array(
              'year' => (int) date('Y', $date),
              'month' => (int) date('m', $date),
              'day' => (int) date('d', $date),
            );
          }
          elseif (($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)) {
            $val = array_pop($val);
          }
          if ($element['#type'] == 'hidden') {
            $element['#value'] = $val;
          }
          $element['#default_value'] = $val;
        }
      }
    }
  }
}