You are here

protected function WebformCivicrmBase::findContact in Webform CiviCRM Integration 8.5

Find an existing contact based on matching criteria Used to populate a webform existing contact field

Parameters

array $component: Webform component of type 'civicrm_contact'

2 calls to WebformCivicrmBase::findContact()
WebformAjax::contactAjax in src/WebformAjax.php
Load one or more contacts via ajax
WebformCivicrmPreProcess::alterForm in src/WebformCivicrmPreProcess.php
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

src/WebformCivicrmBase.php, line 259
Front-end form handler base class.

Class

WebformCivicrmBase
Class WebformCivicrmBase

Namespace

Drupal\webform_civicrm

Code

protected function findContact($component) {
  $contactComponent = \Drupal::service('webform_civicrm.contact_component');
  $utils = \Drupal::service('webform_civicrm.utils');
  $component['#form_key'] = $component['#form_key'] ?? $component['#webform_key'];
  list(, $c, ) = explode('_', $component['#form_key'], 3);
  $filters = $contactComponent
    ->wf_crm_search_filters($this->node, $component);

  // Start with the url - that trumps everything.
  $element_manager = \Drupal::getContainer()
    ->get('plugin.manager.webform.element');
  $existing_component_plugin = $element_manager
    ->getElementInstance($component);
  $allow_url_autofill = $existing_component_plugin
    ->getElementProperty($component, 'allow_url_autofill');
  if ($allow_url_autofill) {
    $query = \Drupal::request()->query;
    if ($query
      ->has("cid{$c}") || $c == 1 && $query
      ->has('cid')) {
      $cid = $query
        ->has("cid{$c}") ? $query
        ->get("cid{$c}") : $query
        ->get('cid');
      if ($cid == 0) {
        $this->ent['contact'][$c]['id'] = $cid;
        return;
      }
      if ($contactComponent
        ->wf_crm_contact_access($component, $filters, $cid) != FALSE) {
        $this->ent['contact'][$c]['id'] = $cid;
      }
    }
  }
  if (empty($this->ent['contact'][$c]['id']) && !empty($component['#default'])) {
    $found = [];
    switch ($component['#default']) {
      case 'user':
        $cid = $utils
          ->wf_crm_user_cid();
        $found = $c == 1 && $cid ? [
          $cid,
        ] : [];
        break;
      case 'contact_id':
        if (isset($component['#default_contact_id'])) {
          $found = [
            $component['#default_contact_id'],
          ];
        }
        break;
      case 'relationship':
        $to = $component['#default_relationship_to'];
        if (!empty($component['#default_relationship']) && !empty($this->ent['contact'][$to]['id'])) {
          $found = $contactComponent
            ->wf_crm_find_relations($this->ent['contact'][$to]['id'], $component['#default_relationship']);
        }
        break;
      case 'auto':
        $component['#allow_create'] = FALSE;
        $found = array_keys($contactComponent
          ->wf_crm_contact_search($this->node, $component, $filters, wf_crm_aval($this->ent, 'contact', [])));
        break;
    }
    if (isset($component['#randomize']) && $component['#randomize']) {
      shuffle($found);
    }
    if (in_array($component['#default'], [
      'user',
      'contact_id',
    ])) {
      $dupes_allowed = TRUE;
    }
    else {
      $dupes_allowed = $component['#dupes_allowed'] ?? 0;
    }
    foreach ($found as $cid) {

      // Don't pick the same contact twice unless explicitly told to do so
      if (!$dupes_allowed) {
        foreach ($this->ent['contact'] as $contact) {
          if (!empty($contact['id']) && $cid == $contact['id']) {
            continue 2;
          }
        }
      }

      // Check filters except for 'auto' which already applied them
      if ($component['#default'] == 'auto' || $contactComponent
        ->wf_crm_contact_access($component, $filters, $cid) != FALSE) {
        $this->ent['contact'][$c]['id'] = $cid;
        break;
      }
    }
  }
}