You are here

public static function CivicrmContact::wf_crm_fill_contact_value in Webform CiviCRM Integration 8.5

Lookup contact name from ID, verify permissions, and attach as html data.

Used when rendering or altering a CiviCRM contact field.

Also sets options for select lists.

Parameters

\Drupal\webform\WebformInterface $node: Node object

array $component: Webform component

array $element: FAPI form element (reference)

array $ids: Known entity ids

1 call to CivicrmContact::wf_crm_fill_contact_value()
WebformCivicrmPreProcess::fillForm in src/WebformCivicrmPreProcess.php
Recursively walk through form array and set properties of CiviCRM fields

File

src/Plugin/WebformElement/CivicrmContact.php, line 525

Class

CivicrmContact
Provides a 'textfield' element.

Namespace

Drupal\webform_civicrm\Plugin\WebformElement

Code

public static function wf_crm_fill_contact_value(WebformInterface $node, array &$element, array $ids = NULL) {
  $cid = wf_crm_aval($element, '#default_value', '');
  $contactComponent = \Drupal::service('webform_civicrm.contact_component');
  if ($element['#type'] == 'hidden') {
    $element['#value'] = $cid;
    if (empty($element['#show_hidden_contact'])) {
      return;
    }
  }
  if ($cid) {

    // Don't lookup same contact again
    if (wf_crm_aval($element, '#attributes:data-civicrm-id') != $cid) {
      $filters = $contactComponent
        ->wf_crm_search_filters($node, $element);
      $name = $contactComponent
        ->wf_crm_contact_access($element, $filters, $cid);
      if ($name !== FALSE) {
        $element['#attributes']['data-civicrm-name'] = $name;
        $element['#attributes']['data-civicrm-id'] = $cid;
      }
      else {
        unset($cid);
      }
    }
  }
  if ($element['#type'] == 'hidden') {
    $element['#theme'] = 'webform_civicrm_contact';
    $element['#attributes']['title'] = $element['#title'];
    $element['#attributes']['cid'] = $cid;
    if (empty($cid) && $element['#none_prompt']) {
      $element['#attributes']['data-civicrm-name'] = Html::escape($element['#none_prompt']);
    }
  }

  // Set options list for select elements. We do this here so we have access to entity ids.
  if (is_array($ids) && $element['#type'] == 'select') {
    $filters = $contactComponent
      ->wf_crm_search_filters($node, $element);
    $element['#options'] = $contactComponent
      ->wf_crm_contact_search($node, $element, $filters, wf_crm_aval($ids, 'contact', []));

    // Display empty option unless there are no results
    if (empty($element['#allow_create']) || count($element['#options']) > 1) {
      $element['#empty_option'] = Xss::filter($element[$element['#options'] ? '#search_prompt' : '#none_prompt']);
    }
  }
}