You are here

public function CivicrmContact::prepare in Webform CiviCRM Integration 8.5

@todo port logic from _webform_render_civicrm_contact()

Overrides WebformElementBase::prepare

See also

_webform_render_civicrm_contact()

File

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

Class

CivicrmContact
Provides a 'textfield' element.

Namespace

Drupal\webform_civicrm\Plugin\WebformElement

Code

public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
  \Drupal::service('civicrm')
    ->initialize();
  $element['#form_key'] = $element['#form_key'] ?? $element['#webform_key'];

  // Webform removes values which equal their defaults but does not populate
  // they keys.
  $ensure_keys_have_values = [
    'hide_method',
    'no_hide_blank',
    'default',
    'default_contact_id',
    'default_relationship',
    'allow_url_autofill',
  ];
  foreach ($ensure_keys_have_values as $key) {
    if (empty($element['#' . $key])) {
      $element['#' . $key] = $this
        ->getDefaultProperty($key);
    }
  }
  $element['#attached']['library'][] = 'webform_civicrm/civicrm_contact';
  $element['#attached']['drupalSettings']['webform_civicrm'][$element['#form_key']] = [
    'hiddenFields' => [],
  ];
  $element['#type'] = $element['#widget'] === 'autocomplete' ? 'textfield' : $element['#widget'];
  $element['#attributes']['data-hide-fields'] = implode(', ', $this
    ->getElementProperty($element, 'hide_fields'));
  $element['#attributes']['data-hide-method'] = $this
    ->getElementProperty($element, 'hide_method');
  $element['#attributes']['data-no-hide-blank'] = (int) $this
    ->getElementProperty($element, 'no_hide_blank');
  $cid = $this
    ->getElementProperty($element, 'default_value');
  list(, $c, ) = explode('_', $element['#form_key'], 3);
  $element['#attributes']['data-civicrm-contact'] = $c;
  $element['#attributes']['data-form-id'] = $webform_submission ? $webform_submission
    ->getWebform()
    ->id() : NULL;
  if ($element['#type'] === 'hidden') {

    // User may not change this value for hidden fields
    $element['#value'] = $cid;
    if (empty($element['#show_hidden_contact'])) {
      return;
    }
    $element['#attributes']['show-hidden-contact'] = 1;
  }
  elseif ($element['#type'] === 'select') {
    $element['#options'] = [];
    $element['#attributes']['data-is-select'] = 1;
  }
  $element['#attributes']['data-search-prompt'] = $this
    ->getElementProperty($element, 'search_prompt') ?? '- Choose existing -';
  $element['#attributes']['data-none-prompt'] = $this
    ->getElementProperty($element, 'none_prompt') ?? '+ Create new +';
  if (!empty($cid)) {
    $webform = $webform_submission
      ->getWebform();
    $contactComponent = \Drupal::service('webform_civicrm.contact_component');

    // Don't lookup same contact again
    if (wf_crm_aval($element, '#attributes:data-civicrm-id') != $cid) {
      $filters = $contactComponent
        ->wf_crm_search_filters($webform, $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 (empty($cid) && $element['#type'] === 'hidden' && isset($element['#none_prompt'])) {
    $element['#attributes']['data-civicrm-name'] = Xss::filter($element['#none_prompt']);
  }
  parent::prepare($element, $webform_submission);
}