You are here

function Utils::wf_crm_state_abbr in Webform CiviCRM Integration 8.5

Match a state/province id to its abbr. and vice-versa

Parameters

$input: User input (state province id or abbr)

$ret: String: 'abbreviation' or 'id'

$country_id: Int: (optional) must be supplied if fetching id from abbr

Return value

string or integer

Overrides UtilsInterface::wf_crm_state_abbr

File

src/Utils.php, line 121
Webform CiviCRM module's common utility functions.

Class

Utils

Namespace

Drupal\webform_civicrm

Code

function wf_crm_state_abbr($input, $ret = 'abbreviation', $country_id = NULL) {
  $params = [
    'sequential' => 1,
  ];
  if ($ret == 'id') {
    if (is_numeric($input)) {
      $state = $this
        ->wf_civicrm_api('state_province', 'getsingle', [
        'return' => 'abbreviation',
        'id' => $input,
      ]);
      $params['abbreviation'] = $state['abbreviation'] ?? $input;
    }
    else {
      $params['abbreviation'] = $input;
    }
    if (!$country_id || $country_id === 'default') {
      $country_id = (int) $this
        ->wf_crm_get_civi_setting('defaultContactCountry', 1228);
    }
  }
  else {
    $params['id'] = $input;
  }
  $params['country_id'] = $country_id;
  $result = $this
    ->wf_crm_apivalues('StateProvince', 'get', $params, $ret);
  return wf_crm_aval($result, 0);
}