You are here

function wf_crm_configure_form_item in Webform CiviCRM Integration 7.3

Build a field item for the configure form

Parameters

$fid: string: civicrm field id

$field: array: Webform field info

$settings: webform_civicrm configuration for this form

Return value

array FAPI form item array for the configure form

1 call to wf_crm_configure_form_item()
wf_crm_configure_form in ./webform_civicrm_admin.inc
Drupal form builder callback Form to configure CiviCRM options for a Webform

File

./webform_civicrm_admin.inc, line 698

Code

function wf_crm_configure_form_item($fid, $field, $settings) {
  list(, $c, $ent, $n, $table, $name) = explode('_', $fid, 6);
  $field['name'] = str_replace('#', $table == 'relationship' ? $n : '', $field['name']);

  // Create dropdown list
  if (!empty($field['expose_list'])) {

    // Retrieve option list
    $options = array(
      'create_civicrm_webform_element' => t('- User Select -'),
    );
    $field['form_key'] = $fid;
    $options += wf_crm_field_options($field, 'config_form', $settings['data']);
    $item = array(
      '#type' => 'select',
      '#title' => $field['name'],
      '#options' => $options,
      '#multiple' => !empty($field['extra']['multiple']),
      '#default_value' => !empty($field['empty_option']) ? 0 : NULL,
    );
    if (isset($field['empty_option'])) {
      $item['#empty_option'] = '- ' . $field['empty_option'] . ' -';
      $item['#empty_value'] = 0;
    }

    // Four ways to get default value...
    // 1: Based on current form state
    if (isset($settings[$fid])) {
      $item['#default_value'] = $settings[$fid];
    }
    elseif (isset($settings['data'][$ent][$c][$table][$n][$name])) {
      $item['#default_value'] = $settings['data'][$ent][$c][$table][$n][$name];
    }
    elseif (isset($field['value'])) {
      $item['#default_value'] = $field['value'];
    }
    elseif (empty($field['extra']['multiple']) && !isset($field['empty_option'])) {
      $options = array_keys($options);
      $item['#default_value'] = $options[1];
    }
    if (!empty($field['extra']['multiple'])) {
      $item['#description'] = t('You may set options here and/or add this element to the webform ("user select"). If you do both, options set here will not appear on the form.');
      $item['#default_value'] = (array) $item['#default_value'];
      if (isset($settings[$fid]) && !is_array($settings[$fid]) && isset($settings['data'][$ent][$c][$table][$n][$name])) {
        $item['#default_value'] += (array) $settings['data'][$ent][$c][$table][$n][$name];
      }
    }
  }
  else {
    $item = array(
      '#type' => 'checkbox',
      '#title' => $field['name'],
      '#return_value' => 'create_civicrm_webform_element',
      '#default_value' => !empty($settings[$fid]),
    );
  }
  if ($d = wf_crm_aval($field, 'extra:description')) {
    $item['#description'] = strlen($d) > 75 ? substr($d, 0, 75) . '...' : $d;
  }
  if ($a = wf_crm_aval($field, 'attributes')) {
    $item['#attributes'] = $a;
  }
  return $item;
}