You are here

public static function AdminForm::insertComponent in Webform CiviCRM Integration 8.5

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

bool $create_fieldsets:

2 calls to AdminForm::insertComponent()
AdminForm::handleDynamicCustomField in src/AdminForm.php
When a custom field is saved/deleted in CiviCRM, sync webforms with dynamic fieldsets.
AdminForm::postProcess in src/AdminForm.php
Submission handler, saves CiviCRM options for a Webform node

File

src/AdminForm.php, line 2206
Webform CiviCRM module's admin form.

Class

AdminForm
@file Webform CiviCRM module's admin form.

Namespace

Drupal\webform_civicrm

Code

public static function insertComponent(&$field, &$enabled, $settings, $create_fieldsets = FALSE) {
  $options = NULL;
  $utils = \Drupal::service('webform_civicrm.utils');
  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' => $utils
        ->wf_crm_contact_label($n, $settings['data']),
    ]) . ' ' . $field['name'];
  }
  else {
    $field['name'] = str_replace(' #', '', $field['name']);
  }
  if ($name == 'contact_sub_type') {
    list($contact_types) = $utils
      ->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['allow_create'] = $a = $utils
      ->wf_crm_name_field_exists($vals, $c, $contact_type);
    $field['none_prompt'] = $a ? t('+ Create new +') : t('- None Found -');
    if ($c == 1 && $contact_type === 'individual') {

      // Default to hidden field for 1st contact
      $field += [
        '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') {

    //To-do: Make this as a default type in src/Fields.php.
    $field['type'] = 'civicrm_options';
    if ($options = $utils
      ->wf_crm_field_options($field, 'component_insert', $settings['data'])) {
      $field['options'] = $options;
      $field['extra']['items'] = $utils
        ->wf_crm_array2str($options);
      $field['extra']['aslist'] = $field['extra']['aslist'] ?? FALSE;
    }
  }
  if ($field['type'] === 'civicrm_contact') {
    $field['contact_type'] = $contact_type;
  }
  if ($field['type'] == 'file') {
    $field['type'] = 'managed_file';
  }
  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']];
  }
  if (!empty($field['set']) && $field['set'] == 'billing_1_number_of_billing') {
    $ent = 'billing_1_number_of_billing';
  }

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

  // Create page break for contribution
  if ($name === 'enable_contribution') {
    $enabled['contribution_pagebreak'] = [
      'type' => 'webform_wizard_page',
      'form_key' => 'contribution_pagebreak',
      'title' => (string) t('Payment'),
    ];
    unset($enabled[$field['form_key']]);
    return;
  }

  // Merge defaults and create webform component
  $field += [
    'extra' => [],
  ];
  if (empty($enabled[$field['form_key']])) {
    $enabled[$field['form_key']] = $field;
  }
}