You are here

function wf_crm_contact_clone in Webform CiviCRM Integration 8.5

Same name and namespace in other branches
  1. 7.5 webform_civicrm.module \wf_crm_contact_clone()
  2. 7.3 webform_civicrm.module \wf_crm_contact_clone()
  3. 7.4 webform_civicrm.module \wf_crm_contact_clone()

Clone a contact via webform. This submit handler is called when cloning a contact's fieldset

File

./webform_civicrm.module, line 516
Webform CiviCRM Integration Module: Links webform submissions to contacts in a CiviCRM database. @author Coleman Watts

Code

function wf_crm_contact_clone($form, $form_state) {
  $fid = $form['form_key']['#default_value'];
  $utils = \Drupal::service('webform_civicrm.utils');
  list(, $old) = $utils
    ->wf_crm_explode_key($fid);
  $node = Node::load($form['nid']['#value']);
  $settings = $node->webform_civicrm;
  $new = count($settings['data']['contact']) + 1;

  // Clone contact
  $settings['data']['contact'][$new] = $settings['data']['contact'][$old];

  // Set label
  $settings['data']['contact'][$new]['contact'][1]['webform_label'] = $form_state['input']['name'];
  $storage = [
    'old' => [
      "civicrm_{$old}_contact_",
    ],
    'new' => [
      "civicrm_{$new}_contact_",
    ],
  ];

  // Clone participant if registering separately
  if (wf_crm_aval($settings['data'], 'participant_reg_type') == 'separate') {
    $settings['data']['participant'][$new] = $settings['data']['participant'][$old];
    $storage['old'][] = "civicrm_{$old}_participant_";
    $storage['new'][] = "civicrm_{$new}_participant_";
  }
  drupal_write_record('webform_civicrm_forms', $settings, 'nid');

  // Store data to rewrite form keys
  wf_crm_contact_clone_storage($storage);
}