You are here

public static function wf_crm_admin_form::insertComponent in Webform CiviCRM Integration 7.5

Same name and namespace in other branches
  1. 7.4 includes/wf_crm_admin_form.inc \wf_crm_admin_form::insertComponent()

Add a CiviCRM field to a webform

Parameters

$field : array: Webform field info

$enabled : array: Array of enabled fields (reference)

$settings: webform_civicrm configuration for this form

stdClass $node:

bool $create_fieldsets:

3 calls to wf_crm_admin_form::insertComponent()
webform_civicrm_update_7300 in ./webform_civicrm.install
Note: There are differences in how contact references and relationships work in the 3.x branch. Read the upgrade instructions at http://drupal.org/node/1615380
wf_crm_admin_form::handleDynamicCustomField in includes/wf_crm_admin_form.inc
When a custom field is saved/deleted in CiviCRM, sync webforms with dynamic fieldsets.
wf_crm_admin_form::postProcess in includes/wf_crm_admin_form.inc
Submission handler, saves CiviCRM options for a Webform node

File

includes/wf_crm_admin_form.inc, line 1848
Webform CiviCRM module's admin form.

Class

wf_crm_admin_form
@file Webform CiviCRM module's admin form.

Code

public static function insertComponent(&$field, &$enabled, $settings, $node, $create_fieldsets = FALSE) {
  $options = NULL;
  list(, $c, $ent, $n, $table, $name) = explode('_', $field['form_key'], 6);
  $contact_type = wf_crm_aval($settings['data']['contact'], "{$c}:contact:1:contact_type");

  // Replace the # token with set number (or append to the end if no token)
  if ($n > 1) {
    if (strpos($field['name'], '#') === FALSE) {
      $field['name'] .= " {$n}";
    }
    else {
      $field['name'] = str_replace('#', $n, $field['name']);
    }
  }
  elseif ($table == 'relationship') {
    $field['name'] = t('Relationship to !contact', [
      '!contact' => wf_crm_contact_label($n, $settings['data']),
    ]) . ' ' . $field['name'];
  }
  else {
    $field['name'] = str_replace(' #', '', $field['name']);
  }
  if ($name == 'contact_sub_type') {
    list($contact_types) = wf_crm_get_contact_types();
    $field['name'] = t('Type of @contact', [
      '@contact' => $contact_types[$contact_type],
    ]);
  }

  // Defaults for existing contact field
  if ($name == 'existing') {
    $vals = $enabled + $settings;

    // Set the allow_create flag based on presence of name or email fields
    $field['extra']['allow_create'] = $a = wf_crm_name_field_exists($vals, $c, $contact_type, $node);
    $field['extra']['none_prompt'] = $a ? t('+ Create new +') : t('- None Found -');
    if ($c == 1 && $contact_type == 'individual') {

      // Default to hidden field for 1st contact
      $field['extra'] += [
        'widget' => 'hidden',
        'default' => 'user',
      ];
    }
  }

  // A width of 20 is more sensible than Drupal's default of 60
  if (($field['type'] == 'textfield' || $field['type'] == 'email') && empty($field['extra']['width'])) {
    $field['extra']['width'] = 20;
  }

  // Support html_textarea module
  if ($field['type'] == 'html_textarea') {
    $field['value']['format'] = filter_default_format();
    $field['value']['value'] = '';
  }

  // Retrieve option list
  if ($field['type'] == 'select') {
    if ($options = wf_crm_field_options($field, 'component_insert', $settings['data'])) {
      $field['extra']['items'] = wf_crm_array2str($options);

      // Default to select widget
      if (!isset($field['extra']['aslist']) && empty($field['extra']['multiple'])) {
        $field['extra']['aslist'] = 1;
      }

      // A single static radio should be shown as a checkbox
      if (count($options) == 1 && empty($field['extra']['aslist']) && empty($field['extra']['civicrm_live_options'])) {
        $field['extra']['multiple'] = 1;
      }
    }
  }
  if (isset($field['value_callback'])) {
    $method = 'get_default_' . $table . '_' . $name;
    $field['value'] = self::$method($field['form_key'], $options);
  }

  // For hidden+select fields such as contribution_page
  if ($field['type'] == 'hidden' && !empty($field['expose_list']) && !empty($settings[$field['form_key']])) {
    $field['value'] = $settings[$field['form_key']];
  }

  // Create fieldsets for multivalued entities
  if ($ent != 'contribution' && $ent != 'lineitem' && ($ent != 'participant' || wf_crm_aval($settings['data'], 'participant_reg_type') == 'separate')) {
    self::addFieldset($c, $field, $enabled, $settings, $ent, $create_fieldsets);
  }

  // Create page break for contribution page
  if ($name == 'contribution_page_id') {
    self::addPageBreak($field);
  }

  // Merge defaults and create webform component
  $field += [
    'extra' => [],
  ];
  if ($defaults = webform_component_invoke($field['type'], 'defaults')) {
    $field += $defaults;
  }
  if (isset($enabled[$field['form_key']])) {
    $field['cid'] = $enabled[$field['form_key']];
    webform_component_update($field);
  }
  else {
    $enabled[$field['form_key']] = webform_component_insert($field);
  }
}