You are here

protected function WebformCivicrmBase::getCustomData in Webform CiviCRM Integration 8.5

Get custom data for an entity

Parameters

$entity_id: Numeric id of entity

$entity_type: Type of crm entity. 'contact' is assumed

$normalize: Default true: if true shift all arrays to start at index 1

Return value

array

3 calls to WebformCivicrmBase::getCustomData()
WebformCivicrmBase::loadContact in src/WebformCivicrmBase.php
Fetch all relevant data for a given contact Used to load contacts for pre-filling a webform, and also to fill in a contact via ajax
WebformCivicrmPreProcess::loadParticipants in src/WebformCivicrmPreProcess.php
Load participant data for a contact
WebformCivicrmPreProcess::populateExistingEntity in src/WebformCivicrmPreProcess.php
Populate existing entity data

File

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

Class

WebformCivicrmBase
Class WebformCivicrmBase

Namespace

Drupal\webform_civicrm

Code

protected function getCustomData($entity_id, $entity_type = NULL, $normalize = TRUE) {
  static $parents = [];
  $utils = \Drupal::service('webform_civicrm.utils');
  if (empty($parents)) {

    // Create matching table to sort fields by group
    foreach ($utils
      ->wf_crm_get_fields() as $key => $value) {
      list($group, $field) = explode('_', $key, 2);
      if (strpos($field, 'custom_') === 0) {
        $parents[$field] = $group;
      }
    }
  }
  $params = [
    'entity_id' => $entity_id,
  ];
  if ($entity_type) {
    $params['entity_table'] = ucfirst($entity_type);
  }
  $result = $utils
    ->wf_crm_apivalues('CustomValue', 'get', $params);
  $values = [];
  foreach ($result as $key => $value) {
    $name = 'custom_' . $key;

    // Sort into groups
    if (isset($parents[$name])) {
      $n = 1;
      foreach ($value as $id => $item) {

        // Non-numeric keys are api extras like "id" and "latest"
        if (is_numeric($id)) {
          $values[$parents[$name]][$normalize ? $n++ : $id][$name] = $item;
        }
      }
    }
  }
  return $values;
}